Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/tcp_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <unistd.h> |
| 46 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | #include <grpc/support/alloc.h> |
| 48 | #include <grpc/support/log.h> |
| 49 | #include <grpc/support/slice.h> |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 50 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | #include <grpc/support/sync.h> |
| 52 | #include <grpc/support/time.h> |
| 53 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 54 | #include "src/core/support/string.h" |
| 55 | #include "src/core/debug/trace.h" |
| 56 | #include "src/core/profiling/timers.h" |
| 57 | |
Craig Tiller | 2da0296 | 2015-05-06 16:14:25 -0700 | [diff] [blame] | 58 | #ifdef GPR_HAVE_MSG_NOSIGNAL |
| 59 | #define SENDMSG_FLAGS MSG_NOSIGNAL |
| 60 | #else |
| 61 | #define SENDMSG_FLAGS 0 |
| 62 | #endif |
| 63 | |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 64 | #ifdef GPR_MSG_IOVLEN_TYPE |
| 65 | typedef GPR_MSG_IOVLEN_TYPE msg_iovlen_type; |
| 66 | #else |
| 67 | typedef size_t msg_iovlen_type; |
| 68 | #endif |
| 69 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 70 | int grpc_tcp_trace = 0; |
| 71 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | grpc_endpoint base; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 74 | grpc_fd *em_fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | int fd; |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 76 | int finished_edge; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | msg_iovlen_type iov_size; /* Number of slices to allocate per read attempt */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | size_t slice_size; |
| 79 | gpr_refcount refcount; |
| 80 | |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 81 | /* garbage after the last read */ |
| 82 | gpr_slice_buffer last_read_buffer; |
| 83 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 84 | gpr_slice_buffer *incoming_buffer; |
| 85 | gpr_slice_buffer *outgoing_buffer; |
| 86 | /** slice within outgoing_buffer to write next */ |
| 87 | size_t outgoing_slice_idx; |
| 88 | /** byte within outgoing_buffer->slices[outgoing_slice_idx] to write next */ |
| 89 | size_t outgoing_byte_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 91 | grpc_closure *read_cb; |
| 92 | grpc_closure *write_cb; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 93 | grpc_closure *release_fd_cb; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame^] | 94 | int *release_fd; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 95 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 96 | grpc_closure read_closure; |
| 97 | grpc_closure write_closure; |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 98 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 99 | char *peer_string; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | } grpc_tcp; |
| 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
| 103 | int success); |
| 104 | static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
| 105 | int success); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
| 108 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 109 | grpc_fd_shutdown(exec_ctx, tcp->em_fd); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame^] | 113 | grpc_fd_orphan(exec_ctx, tcp->em_fd, tcp->release_fd_cb, tcp->release_fd, |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 114 | "tcp_unref_orphan"); |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 115 | gpr_slice_buffer_destroy(&tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | gpr_free(tcp->peer_string); |
| 117 | gpr_free(tcp); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /*#define GRPC_TCP_REFCOUNT_DEBUG*/ |
| 121 | #ifdef GRPC_TCP_REFCOUNT_DEBUG |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 122 | #define TCP_UNREF(cl, tcp, reason) \ |
| 123 | tcp_unref((cl), (tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 124 | #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, |
| 126 | const char *reason, const char *file, int line) { |
| 127 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp, |
| 128 | reason, tcp->refcount.count, tcp->refcount.count - 1); |
| 129 | if (gpr_unref(&tcp->refcount)) { |
| 130 | tcp_free(exec_ctx, tcp); |
| 131 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file, |
| 135 | int line) { |
| 136 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp, |
| 137 | reason, tcp->refcount.count, tcp->refcount.count + 1); |
| 138 | gpr_ref(&tcp->refcount); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 139 | } |
| 140 | #else |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 141 | #define TCP_UNREF(cl, tcp, reason) tcp_unref((cl), (tcp)) |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 142 | #define TCP_REF(tcp, reason) tcp_ref((tcp)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 143 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
| 144 | if (gpr_unref(&tcp->refcount)) { |
| 145 | tcp_free(exec_ctx, tcp); |
| 146 | } |
Craig Tiller | 592e7f2 | 2015-08-21 10:45:48 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 150 | #endif |
| 151 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | static void tcp_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
| 153 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 154 | TCP_UNREF(exec_ctx, tcp, "destroy"); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | static void call_read_cb(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, int success) { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 158 | grpc_closure *cb = tcp->read_cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | if (grpc_tcp_trace) { |
| 161 | size_t i; |
| 162 | gpr_log(GPR_DEBUG, "read: success=%d", success); |
| 163 | for (i = 0; i < tcp->incoming_buffer->count; i++) { |
| 164 | char *dump = gpr_dump_slice(tcp->incoming_buffer->slices[i], |
| 165 | GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 166 | gpr_log(GPR_DEBUG, "READ %p: %s", tcp, dump); |
| 167 | gpr_free(dump); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 168 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | |
| 171 | tcp->read_cb = NULL; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 172 | tcp->incoming_buffer = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | cb->cb(exec_ctx, cb->cb_arg, success); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | #define MAX_READ_IOVEC 4 |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | struct msghdr msg; |
| 179 | struct iovec iov[MAX_READ_IOVEC]; |
| 180 | ssize_t read_bytes; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 181 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | GPR_ASSERT(!tcp->finished_edge); |
| 184 | GPR_ASSERT(tcp->iov_size <= MAX_READ_IOVEC); |
| 185 | GPR_ASSERT(tcp->incoming_buffer->count <= MAX_READ_IOVEC); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 186 | GPR_TIMER_BEGIN("tcp_continue_read", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 188 | while (tcp->incoming_buffer->count < (size_t)tcp->iov_size) { |
| 189 | gpr_slice_buffer_add_indexed(tcp->incoming_buffer, |
| 190 | gpr_slice_malloc(tcp->slice_size)); |
| 191 | } |
| 192 | for (i = 0; i < tcp->incoming_buffer->count; i++) { |
| 193 | iov[i].iov_base = GPR_SLICE_START_PTR(tcp->incoming_buffer->slices[i]); |
| 194 | iov[i].iov_len = GPR_SLICE_LENGTH(tcp->incoming_buffer->slices[i]); |
| 195 | } |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 196 | |
| 197 | msg.msg_name = NULL; |
| 198 | msg.msg_namelen = 0; |
| 199 | msg.msg_iov = iov; |
| 200 | msg.msg_iovlen = tcp->iov_size; |
| 201 | msg.msg_control = NULL; |
| 202 | msg.msg_controllen = 0; |
| 203 | msg.msg_flags = 0; |
| 204 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 205 | GPR_TIMER_BEGIN("recvmsg", 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | do { |
| 207 | read_bytes = recvmsg(tcp->fd, &msg, 0); |
| 208 | } while (read_bytes < 0 && errno == EINTR); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 209 | GPR_TIMER_END("recvmsg", 0); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | if (read_bytes < 0) { |
| 212 | /* NB: After calling call_read_cb a parallel call of the read handler may |
| 213 | * be running. */ |
| 214 | if (errno == EAGAIN) { |
| 215 | if (tcp->iov_size > 1) { |
| 216 | tcp->iov_size /= 2; |
| 217 | } |
| 218 | /* We've consumed the edge, request a new one */ |
| 219 | grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure); |
| 220 | } else { |
| 221 | /* TODO(klempner): Log interesting errors */ |
| 222 | gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer); |
| 223 | call_read_cb(exec_ctx, tcp, 0); |
| 224 | TCP_UNREF(exec_ctx, tcp, "read"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 225 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 226 | } else if (read_bytes == 0) { |
| 227 | /* 0 read size ==> end of stream */ |
| 228 | gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer); |
| 229 | call_read_cb(exec_ctx, tcp, 0); |
| 230 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 231 | } else { |
| 232 | GPR_ASSERT((size_t)read_bytes <= tcp->incoming_buffer->length); |
| 233 | if ((size_t)read_bytes < tcp->incoming_buffer->length) { |
| 234 | gpr_slice_buffer_trim_end( |
| 235 | tcp->incoming_buffer, |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 236 | tcp->incoming_buffer->length - (size_t)read_bytes, |
| 237 | &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 238 | } else if (tcp->iov_size < MAX_READ_IOVEC) { |
| 239 | ++tcp->iov_size; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 240 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | GPR_ASSERT((size_t)read_bytes == tcp->incoming_buffer->length); |
| 242 | call_read_cb(exec_ctx, tcp, 1); |
| 243 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 244 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 245 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 246 | GPR_TIMER_END("tcp_continue_read", 0); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
| 250 | int success) { |
| 251 | grpc_tcp *tcp = (grpc_tcp *)arg; |
| 252 | GPR_ASSERT(!tcp->finished_edge); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 253 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 254 | if (!success) { |
| 255 | gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer); |
| 256 | call_read_cb(exec_ctx, tcp, 0); |
| 257 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 258 | } else { |
| 259 | tcp_continue_read(exec_ctx, tcp); |
| 260 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 263 | static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 264 | gpr_slice_buffer *incoming_buffer, grpc_closure *cb) { |
| 265 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 266 | GPR_ASSERT(tcp->read_cb == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 267 | tcp->read_cb = cb; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 268 | tcp->incoming_buffer = incoming_buffer; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 269 | gpr_slice_buffer_reset_and_unref(incoming_buffer); |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 270 | gpr_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | TCP_REF(tcp, "read"); |
| 272 | if (tcp->finished_edge) { |
| 273 | tcp->finished_edge = 0; |
| 274 | grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure); |
| 275 | } else { |
| 276 | grpc_exec_ctx_enqueue(exec_ctx, &tcp->read_closure, 1); |
| 277 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | typedef enum { FLUSH_DONE, FLUSH_PENDING, FLUSH_ERROR } flush_result; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 281 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 282 | #define MAX_WRITE_IOVEC 16 |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 283 | static flush_result tcp_flush(grpc_tcp *tcp) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | struct msghdr msg; |
| 285 | struct iovec iov[MAX_WRITE_IOVEC]; |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 286 | msg_iovlen_type iov_size; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | ssize_t sent_length; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 288 | size_t sending_length; |
| 289 | size_t trailing; |
| 290 | size_t unwind_slice_idx; |
| 291 | size_t unwind_byte_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 292 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | for (;;) { |
| 294 | sending_length = 0; |
| 295 | unwind_slice_idx = tcp->outgoing_slice_idx; |
| 296 | unwind_byte_idx = tcp->outgoing_byte_idx; |
| 297 | for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count && |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 298 | iov_size != MAX_WRITE_IOVEC; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 299 | iov_size++) { |
| 300 | iov[iov_size].iov_base = |
| 301 | GPR_SLICE_START_PTR( |
| 302 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) + |
| 303 | tcp->outgoing_byte_idx; |
| 304 | iov[iov_size].iov_len = |
| 305 | GPR_SLICE_LENGTH( |
| 306 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) - |
| 307 | tcp->outgoing_byte_idx; |
| 308 | sending_length += iov[iov_size].iov_len; |
| 309 | tcp->outgoing_slice_idx++; |
| 310 | tcp->outgoing_byte_idx = 0; |
| 311 | } |
| 312 | GPR_ASSERT(iov_size > 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | msg.msg_name = NULL; |
| 315 | msg.msg_namelen = 0; |
| 316 | msg.msg_iov = iov; |
| 317 | msg.msg_iovlen = iov_size; |
| 318 | msg.msg_control = NULL; |
| 319 | msg.msg_controllen = 0; |
| 320 | msg.msg_flags = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 321 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 322 | GPR_TIMER_BEGIN("sendmsg", 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 323 | do { |
| 324 | /* TODO(klempner): Cork if this is a partial write */ |
| 325 | sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS); |
| 326 | } while (sent_length < 0 && errno == EINTR); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 327 | GPR_TIMER_END("sendmsg", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 328 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 329 | if (sent_length < 0) { |
| 330 | if (errno == EAGAIN) { |
| 331 | tcp->outgoing_slice_idx = unwind_slice_idx; |
| 332 | tcp->outgoing_byte_idx = unwind_byte_idx; |
| 333 | return FLUSH_PENDING; |
| 334 | } else { |
| 335 | /* TODO(klempner): Log some of these */ |
| 336 | return FLUSH_ERROR; |
| 337 | } |
| 338 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 339 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | GPR_ASSERT(tcp->outgoing_byte_idx == 0); |
| 341 | trailing = sending_length - (size_t)sent_length; |
| 342 | while (trailing > 0) { |
| 343 | size_t slice_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | tcp->outgoing_slice_idx--; |
| 346 | slice_length = GPR_SLICE_LENGTH( |
| 347 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]); |
| 348 | if (slice_length > trailing) { |
| 349 | tcp->outgoing_byte_idx = slice_length - trailing; |
| 350 | break; |
| 351 | } else { |
| 352 | trailing -= slice_length; |
| 353 | } |
| 354 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 355 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) { |
| 357 | return FLUSH_DONE; |
| 358 | } |
| 359 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
| 363 | int success) { |
| 364 | grpc_tcp *tcp = (grpc_tcp *)arg; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 365 | flush_result status; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 366 | grpc_closure *cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 367 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | if (!success) { |
| 369 | cb = tcp->write_cb; |
| 370 | tcp->write_cb = NULL; |
| 371 | cb->cb(exec_ctx, cb->cb_arg, 0); |
| 372 | TCP_UNREF(exec_ctx, tcp, "write"); |
| 373 | return; |
| 374 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 375 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 376 | status = tcp_flush(tcp); |
| 377 | if (status == FLUSH_PENDING) { |
| 378 | grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); |
| 379 | } else { |
| 380 | cb = tcp->write_cb; |
| 381 | tcp->write_cb = NULL; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 382 | GPR_TIMER_BEGIN("tcp_handle_write.cb", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 383 | cb->cb(exec_ctx, cb->cb_arg, status == FLUSH_DONE); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 384 | GPR_TIMER_END("tcp_handle_write.cb", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 385 | TCP_UNREF(exec_ctx, tcp, "write"); |
| 386 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 389 | static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 390 | gpr_slice_buffer *buf, grpc_closure *cb) { |
| 391 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 392 | flush_result status; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 393 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 394 | if (grpc_tcp_trace) { |
| 395 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 396 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | for (i = 0; i < buf->count; i++) { |
| 398 | char *data = |
| 399 | gpr_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 400 | gpr_log(GPR_DEBUG, "WRITE %p: %s", tcp, data); |
| 401 | gpr_free(data); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 402 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 403 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 404 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 405 | GPR_TIMER_BEGIN("tcp_write", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | GPR_ASSERT(tcp->write_cb == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 407 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 408 | if (buf->length == 0) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 409 | GPR_TIMER_END("tcp_write", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 410 | grpc_exec_ctx_enqueue(exec_ctx, cb, 1); |
| 411 | return; |
| 412 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 413 | tcp->outgoing_buffer = buf; |
| 414 | tcp->outgoing_slice_idx = 0; |
| 415 | tcp->outgoing_byte_idx = 0; |
| 416 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 417 | status = tcp_flush(tcp); |
| 418 | if (status == FLUSH_PENDING) { |
| 419 | TCP_REF(tcp, "write"); |
| 420 | tcp->write_cb = cb; |
| 421 | grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); |
| 422 | } else { |
| 423 | grpc_exec_ctx_enqueue(exec_ctx, cb, status == FLUSH_DONE); |
| 424 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 425 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 426 | GPR_TIMER_END("tcp_write", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 429 | static void tcp_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 430 | grpc_pollset *pollset) { |
| 431 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 432 | grpc_pollset_add_fd(exec_ctx, pollset, tcp->em_fd); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | static void tcp_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 436 | grpc_pollset_set *pollset_set) { |
| 437 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 438 | grpc_pollset_set_add_fd(exec_ctx, pollset_set, tcp->em_fd); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 441 | static char *tcp_get_peer(grpc_endpoint *ep) { |
| 442 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 443 | return gpr_strdup(tcp->peer_string); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 444 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 445 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 446 | static const grpc_endpoint_vtable vtable = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 447 | tcp_read, tcp_write, tcp_add_to_pollset, tcp_add_to_pollset_set, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 448 | tcp_shutdown, tcp_destroy, tcp_get_peer}; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 449 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 450 | grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size, |
| 451 | const char *peer_string) { |
| 452 | grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 453 | tcp->base.vtable = &vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 454 | tcp->peer_string = gpr_strdup(peer_string); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 455 | tcp->fd = em_fd->fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 456 | tcp->read_cb = NULL; |
| 457 | tcp->write_cb = NULL; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 458 | tcp->release_fd_cb = NULL; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame^] | 459 | tcp->release_fd = NULL; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 460 | tcp->incoming_buffer = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 461 | tcp->slice_size = slice_size; |
Craig Tiller | 2b1fd66 | 2015-04-28 07:33:37 -0700 | [diff] [blame] | 462 | tcp->iov_size = 1; |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 463 | tcp->finished_edge = 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 464 | /* paired with unref in grpc_tcp_destroy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 465 | gpr_ref_init(&tcp->refcount, 1); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 466 | tcp->em_fd = em_fd; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 467 | tcp->read_closure.cb = tcp_handle_read; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 468 | tcp->read_closure.cb_arg = tcp; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 469 | tcp->write_closure.cb = tcp_handle_write; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 470 | tcp->write_closure.cb_arg = tcp; |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 471 | gpr_slice_buffer_init(&tcp->last_read_buffer); |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 472 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 473 | return &tcp->base; |
| 474 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 475 | |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 476 | void grpc_tcp_destroy_and_release_fd(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame^] | 477 | int *fd, grpc_closure *done) { |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 478 | grpc_tcp *tcp = (grpc_tcp *)ep; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame^] | 479 | GPR_ASSERT(ep->vtable == &vtable); |
| 480 | tcp->release_fd = fd; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 481 | tcp->release_fd_cb = done; |
| 482 | TCP_UNREF(exec_ctx, tcp, "destroy"); |
| 483 | } |
| 484 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 485 | #endif |