Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
murgatroid99 | 5407089 | 2016-08-08 17:01:18 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/port.h" |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 35 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 36 | #ifdef GRPC_POSIX_SOCKET |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 37 | |
Makarand Dharmapurikar | 0579cfc | 2016-06-20 15:45:24 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/network_status_tracker.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 39 | #include "src/core/lib/iomgr/tcp_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | |
| 41 | #include <errno.h> |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 42 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <sys/socket.h> |
Craig Tiller | a8be91b | 2016-02-19 16:22:44 -0800 | [diff] [blame] | 46 | #include <sys/types.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | #include <unistd.h> |
| 48 | |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 49 | #include <grpc/slice.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | #include <grpc/support/alloc.h> |
| 51 | #include <grpc/support/log.h> |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 52 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | #include <grpc/support/sync.h> |
| 54 | #include <grpc/support/time.h> |
| 55 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 56 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 8a03448 | 2016-03-28 16:09:04 -0700 | [diff] [blame] | 57 | #include "src/core/lib/iomgr/ev_posix.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 58 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 59 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 60 | #include "src/core/lib/slice/slice_string_helpers.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 61 | #include "src/core/lib/support/string.h" |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 62 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 63 | #ifdef GRPC_HAVE_MSG_NOSIGNAL |
Craig Tiller | 2da0296 | 2015-05-06 16:14:25 -0700 | [diff] [blame] | 64 | #define SENDMSG_FLAGS MSG_NOSIGNAL |
| 65 | #else |
| 66 | #define SENDMSG_FLAGS 0 |
| 67 | #endif |
| 68 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 69 | #ifdef GRPC_MSG_IOVLEN_TYPE |
| 70 | typedef GRPC_MSG_IOVLEN_TYPE msg_iovlen_type; |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 71 | #else |
| 72 | typedef size_t msg_iovlen_type; |
| 73 | #endif |
| 74 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 75 | int grpc_tcp_trace = 0; |
| 76 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | grpc_endpoint base; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 79 | grpc_fd *em_fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | int fd; |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 81 | bool finished_edge; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | 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] | 83 | size_t slice_size; |
| 84 | gpr_refcount refcount; |
Craig Tiller | e9f385a | 2016-09-26 14:54:43 -0700 | [diff] [blame] | 85 | gpr_atm shutdown_count; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 87 | /* garbage after the last read */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 88 | grpc_slice_buffer last_read_buffer; |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 89 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 90 | grpc_slice_buffer *incoming_buffer; |
| 91 | grpc_slice_buffer *outgoing_buffer; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 92 | /** slice within outgoing_buffer to write next */ |
| 93 | size_t outgoing_slice_idx; |
| 94 | /** byte within outgoing_buffer->slices[outgoing_slice_idx] to write next */ |
| 95 | size_t outgoing_byte_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 97 | grpc_closure *read_cb; |
| 98 | grpc_closure *write_cb; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 99 | grpc_closure *release_fd_cb; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 100 | int *release_fd; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 101 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 102 | grpc_closure read_closure; |
| 103 | grpc_closure write_closure; |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 104 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 105 | char *peer_string; |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 106 | |
Craig Tiller | a947f1c | 2016-11-04 13:53:17 -0700 | [diff] [blame] | 107 | grpc_resource_user *resource_user; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 108 | grpc_resource_user_slice_allocator slice_allocator; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | } grpc_tcp; |
| 110 | |
Craig Tiller | db25f08 | 2016-12-02 12:45:24 -0800 | [diff] [blame] | 111 | static grpc_error *tcp_annotate_error(grpc_error *src_error, grpc_tcp *tcp) { |
| 112 | return grpc_error_set_str( |
| 113 | grpc_error_set_int(src_error, GRPC_ERROR_INT_FD, tcp->fd), |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 114 | GRPC_ERROR_STR_TARGET_ADDRESS, |
| 115 | grpc_slice_from_copied_string(tcp->peer_string)); |
Craig Tiller | db25f08 | 2016-12-02 12:45:24 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 119 | grpc_error *error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 121 | grpc_error *error); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | |
Craig Tiller | cda759d | 2017-01-27 11:37:37 -0800 | [diff] [blame] | 123 | static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 124 | grpc_error *why) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Craig Tiller | cda759d | 2017-01-27 11:37:37 -0800 | [diff] [blame] | 126 | grpc_fd_shutdown(exec_ctx, tcp->em_fd, why); |
Craig Tiller | a947f1c | 2016-11-04 13:53:17 -0700 | [diff] [blame] | 127 | grpc_resource_user_shutdown(exec_ctx, tcp->resource_user); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Craig Tiller | e9f385a | 2016-09-26 14:54:43 -0700 | [diff] [blame] | 130 | static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 131 | 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] | 132 | "tcp_unref_orphan"); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 133 | grpc_slice_buffer_destroy_internal(exec_ctx, &tcp->last_read_buffer); |
Craig Tiller | a947f1c | 2016-11-04 13:53:17 -0700 | [diff] [blame] | 134 | grpc_resource_user_unref(exec_ctx, tcp->resource_user); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | gpr_free(tcp->peer_string); |
Craig Tiller | e9f385a | 2016-09-26 14:54:43 -0700 | [diff] [blame] | 136 | gpr_free(tcp); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /*#define GRPC_TCP_REFCOUNT_DEBUG*/ |
| 140 | #ifdef GRPC_TCP_REFCOUNT_DEBUG |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 141 | #define TCP_UNREF(cl, tcp, reason) \ |
| 142 | tcp_unref((cl), (tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 143 | #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, |
| 145 | const char *reason, const char *file, int line) { |
| 146 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp, |
| 147 | reason, tcp->refcount.count, tcp->refcount.count - 1); |
| 148 | if (gpr_unref(&tcp->refcount)) { |
Craig Tiller | e9f385a | 2016-09-26 14:54:43 -0700 | [diff] [blame] | 149 | tcp_free(exec_ctx, tcp); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file, |
| 154 | int line) { |
| 155 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp, |
| 156 | reason, tcp->refcount.count, tcp->refcount.count + 1); |
| 157 | gpr_ref(&tcp->refcount); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 158 | } |
| 159 | #else |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 160 | #define TCP_UNREF(cl, tcp, reason) tcp_unref((cl), (tcp)) |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 161 | #define TCP_REF(tcp, reason) tcp_ref((tcp)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
| 163 | if (gpr_unref(&tcp->refcount)) { |
Craig Tiller | e9f385a | 2016-09-26 14:54:43 -0700 | [diff] [blame] | 164 | tcp_free(exec_ctx, tcp); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | } |
Craig Tiller | 592e7f2 | 2015-08-21 10:45:48 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 169 | #endif |
| 170 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | static void tcp_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
Makarand Dharmapurikar | 6149443 | 2016-06-22 13:34:59 -0700 | [diff] [blame] | 172 | grpc_network_status_unregister_endpoint(ep); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 174 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 175 | TCP_UNREF(exec_ctx, tcp, "destroy"); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 178 | static void call_read_cb(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, |
| 179 | grpc_error *error) { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 180 | grpc_closure *cb = tcp->read_cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | |
Craig Tiller | 44f6245 | 2016-06-23 08:22:30 -0700 | [diff] [blame] | 182 | if (grpc_tcp_trace) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | size_t i; |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 184 | const char *str = grpc_error_string(error); |
| 185 | gpr_log(GPR_DEBUG, "read: error=%s", str); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 186 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | for (i = 0; i < tcp->incoming_buffer->count; i++) { |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 188 | char *dump = grpc_dump_slice(tcp->incoming_buffer->slices[i], |
| 189 | GPR_DUMP_HEX | GPR_DUMP_ASCII); |
Mark D. Roth | bc84672 | 2016-05-04 10:53:50 -0700 | [diff] [blame] | 190 | gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string, dump); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 191 | gpr_free(dump); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 192 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | |
| 195 | tcp->read_cb = NULL; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 196 | tcp->incoming_buffer = NULL; |
Craig Tiller | de2c41c | 2016-09-01 15:08:08 -0700 | [diff] [blame] | 197 | grpc_closure_run(exec_ctx, cb, error); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | #define MAX_READ_IOVEC 4 |
Craig Tiller | 1f8d1d5 | 2016-09-26 10:16:14 -0700 | [diff] [blame] | 201 | static void tcp_do_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 202 | struct msghdr msg; |
| 203 | struct iovec iov[MAX_READ_IOVEC]; |
| 204 | ssize_t read_bytes; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 205 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 207 | GPR_ASSERT(!tcp->finished_edge); |
| 208 | GPR_ASSERT(tcp->iov_size <= MAX_READ_IOVEC); |
| 209 | GPR_ASSERT(tcp->incoming_buffer->count <= MAX_READ_IOVEC); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 210 | GPR_TIMER_BEGIN("tcp_continue_read", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 211 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 212 | for (i = 0; i < tcp->incoming_buffer->count; i++) { |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 213 | iov[i].iov_base = GRPC_SLICE_START_PTR(tcp->incoming_buffer->slices[i]); |
| 214 | iov[i].iov_len = GRPC_SLICE_LENGTH(tcp->incoming_buffer->slices[i]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 215 | } |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 216 | |
| 217 | msg.msg_name = NULL; |
| 218 | msg.msg_namelen = 0; |
| 219 | msg.msg_iov = iov; |
| 220 | msg.msg_iovlen = tcp->iov_size; |
| 221 | msg.msg_control = NULL; |
| 222 | msg.msg_controllen = 0; |
| 223 | msg.msg_flags = 0; |
| 224 | |
Craig Tiller | ca04562 | 2016-08-09 08:38:03 -0700 | [diff] [blame] | 225 | GPR_TIMER_BEGIN("recvmsg", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 226 | do { |
| 227 | read_bytes = recvmsg(tcp->fd, &msg, 0); |
| 228 | } while (read_bytes < 0 && errno == EINTR); |
Craig Tiller | ca04562 | 2016-08-09 08:38:03 -0700 | [diff] [blame] | 229 | GPR_TIMER_END("recvmsg", read_bytes >= 0); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 230 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 231 | if (read_bytes < 0) { |
| 232 | /* NB: After calling call_read_cb a parallel call of the read handler may |
| 233 | * be running. */ |
| 234 | if (errno == EAGAIN) { |
| 235 | if (tcp->iov_size > 1) { |
| 236 | tcp->iov_size /= 2; |
| 237 | } |
| 238 | /* We've consumed the edge, request a new one */ |
| 239 | grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure); |
| 240 | } else { |
Craig Tiller | 298d481 | 2016-12-09 08:51:48 -0800 | [diff] [blame] | 241 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, |
| 242 | tcp->incoming_buffer); |
Craig Tiller | db25f08 | 2016-12-02 12:45:24 -0800 | [diff] [blame] | 243 | call_read_cb(exec_ctx, tcp, |
| 244 | tcp_annotate_error(GRPC_OS_ERROR(errno, "recvmsg"), tcp)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | TCP_UNREF(exec_ctx, tcp, "read"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 246 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | } else if (read_bytes == 0) { |
| 248 | /* 0 read size ==> end of stream */ |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 249 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, tcp->incoming_buffer); |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 250 | call_read_cb( |
| 251 | exec_ctx, tcp, |
| 252 | tcp_annotate_error( |
| 253 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Socket closed"), tcp)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 254 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 255 | } else { |
| 256 | GPR_ASSERT((size_t)read_bytes <= tcp->incoming_buffer->length); |
| 257 | if ((size_t)read_bytes < tcp->incoming_buffer->length) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 258 | grpc_slice_buffer_trim_end( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 259 | tcp->incoming_buffer, |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 260 | tcp->incoming_buffer->length - (size_t)read_bytes, |
| 261 | &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 262 | } else if (tcp->iov_size < MAX_READ_IOVEC) { |
| 263 | ++tcp->iov_size; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 264 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 265 | GPR_ASSERT((size_t)read_bytes == tcp->incoming_buffer->length); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 266 | call_read_cb(exec_ctx, tcp, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 267 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 268 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 269 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 270 | GPR_TIMER_END("tcp_continue_read", 0); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Craig Tiller | 091e4f0 | 2016-09-29 09:57:35 -0700 | [diff] [blame] | 273 | static void tcp_read_allocation_done(grpc_exec_ctx *exec_ctx, void *tcpp, |
Craig Tiller | 1f8d1d5 | 2016-09-26 10:16:14 -0700 | [diff] [blame] | 274 | grpc_error *error) { |
Craig Tiller | 091e4f0 | 2016-09-29 09:57:35 -0700 | [diff] [blame] | 275 | grpc_tcp *tcp = tcpp; |
| 276 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 277 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, tcp->incoming_buffer); |
| 278 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, |
| 279 | &tcp->last_read_buffer); |
Craig Tiller | 091e4f0 | 2016-09-29 09:57:35 -0700 | [diff] [blame] | 280 | call_read_cb(exec_ctx, tcp, GRPC_ERROR_REF(error)); |
| 281 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 282 | } else { |
| 283 | tcp_do_read(exec_ctx, tcp); |
| 284 | } |
Craig Tiller | 1f8d1d5 | 2016-09-26 10:16:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
| 288 | if (tcp->incoming_buffer->count < (size_t)tcp->iov_size) { |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 289 | grpc_resource_user_alloc_slices( |
Craig Tiller | 1f8d1d5 | 2016-09-26 10:16:14 -0700 | [diff] [blame] | 290 | exec_ctx, &tcp->slice_allocator, tcp->slice_size, |
| 291 | (size_t)tcp->iov_size - tcp->incoming_buffer->count, |
| 292 | tcp->incoming_buffer); |
| 293 | } else { |
| 294 | tcp_do_read(exec_ctx, tcp); |
| 295 | } |
| 296 | } |
| 297 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 299 | grpc_error *error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 300 | grpc_tcp *tcp = (grpc_tcp *)arg; |
| 301 | GPR_ASSERT(!tcp->finished_edge); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 302 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 303 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 304 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, tcp->incoming_buffer); |
| 305 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, |
| 306 | &tcp->last_read_buffer); |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 307 | call_read_cb(exec_ctx, tcp, GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 308 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 309 | } else { |
| 310 | tcp_continue_read(exec_ctx, tcp); |
| 311 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 315 | grpc_slice_buffer *incoming_buffer, grpc_closure *cb) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 316 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 317 | GPR_ASSERT(tcp->read_cb == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 318 | tcp->read_cb = cb; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 319 | tcp->incoming_buffer = incoming_buffer; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 320 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, incoming_buffer); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 321 | grpc_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 322 | TCP_REF(tcp, "read"); |
| 323 | if (tcp->finished_edge) { |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 324 | tcp->finished_edge = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 325 | grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure); |
| 326 | } else { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 327 | grpc_closure_sched(exec_ctx, &tcp->read_closure, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 328 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 331 | /* returns true if done, false if pending; if returning true, *error is set */ |
Vijay Pai | 8f76df4 | 2016-07-29 08:31:20 -0700 | [diff] [blame] | 332 | #define MAX_WRITE_IOVEC 1000 |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 333 | static bool tcp_flush(grpc_tcp *tcp, grpc_error **error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 334 | struct msghdr msg; |
| 335 | struct iovec iov[MAX_WRITE_IOVEC]; |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 336 | msg_iovlen_type iov_size; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 337 | ssize_t sent_length; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 338 | size_t sending_length; |
| 339 | size_t trailing; |
| 340 | size_t unwind_slice_idx; |
| 341 | size_t unwind_byte_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 342 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | for (;;) { |
| 344 | sending_length = 0; |
| 345 | unwind_slice_idx = tcp->outgoing_slice_idx; |
| 346 | unwind_byte_idx = tcp->outgoing_byte_idx; |
| 347 | for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count && |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 348 | iov_size != MAX_WRITE_IOVEC; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 349 | iov_size++) { |
| 350 | iov[iov_size].iov_base = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 351 | GRPC_SLICE_START_PTR( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 352 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) + |
| 353 | tcp->outgoing_byte_idx; |
| 354 | iov[iov_size].iov_len = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 355 | GRPC_SLICE_LENGTH( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) - |
| 357 | tcp->outgoing_byte_idx; |
| 358 | sending_length += iov[iov_size].iov_len; |
| 359 | tcp->outgoing_slice_idx++; |
| 360 | tcp->outgoing_byte_idx = 0; |
| 361 | } |
| 362 | GPR_ASSERT(iov_size > 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 363 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 364 | msg.msg_name = NULL; |
| 365 | msg.msg_namelen = 0; |
| 366 | msg.msg_iov = iov; |
| 367 | msg.msg_iovlen = iov_size; |
| 368 | msg.msg_control = NULL; |
| 369 | msg.msg_controllen = 0; |
| 370 | msg.msg_flags = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 371 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 372 | GPR_TIMER_BEGIN("sendmsg", 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 373 | do { |
| 374 | /* TODO(klempner): Cork if this is a partial write */ |
| 375 | sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS); |
| 376 | } while (sent_length < 0 && errno == EINTR); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 377 | GPR_TIMER_END("sendmsg", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 378 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | if (sent_length < 0) { |
| 380 | if (errno == EAGAIN) { |
| 381 | tcp->outgoing_slice_idx = unwind_slice_idx; |
| 382 | tcp->outgoing_byte_idx = unwind_byte_idx; |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 383 | return false; |
Ken Payson | 1b1a860 | 2016-11-14 10:10:47 -0800 | [diff] [blame] | 384 | } else if (errno == EPIPE) { |
| 385 | *error = grpc_error_set_int(GRPC_OS_ERROR(errno, "sendmsg"), |
| 386 | GRPC_ERROR_INT_GRPC_STATUS, |
| 387 | GRPC_STATUS_UNAVAILABLE); |
| 388 | return true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 389 | } else { |
Craig Tiller | db25f08 | 2016-12-02 12:45:24 -0800 | [diff] [blame] | 390 | *error = tcp_annotate_error(GRPC_OS_ERROR(errno, "sendmsg"), tcp); |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 391 | return true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 394 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 395 | GPR_ASSERT(tcp->outgoing_byte_idx == 0); |
| 396 | trailing = sending_length - (size_t)sent_length; |
| 397 | while (trailing > 0) { |
| 398 | size_t slice_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 399 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 400 | tcp->outgoing_slice_idx--; |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 401 | slice_length = GRPC_SLICE_LENGTH( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]); |
| 403 | if (slice_length > trailing) { |
| 404 | tcp->outgoing_byte_idx = slice_length - trailing; |
| 405 | break; |
| 406 | } else { |
| 407 | trailing -= slice_length; |
| 408 | } |
| 409 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 410 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) { |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 412 | *error = GRPC_ERROR_NONE; |
| 413 | return true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 414 | } |
| 415 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 418 | static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 419 | grpc_error *error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 420 | grpc_tcp *tcp = (grpc_tcp *)arg; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 421 | grpc_closure *cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 422 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 423 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 424 | cb = tcp->write_cb; |
| 425 | tcp->write_cb = NULL; |
Mark D. Roth | b06c640 | 2016-05-13 12:39:30 -0700 | [diff] [blame] | 426 | cb->cb(exec_ctx, cb->cb_arg, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 427 | TCP_UNREF(exec_ctx, tcp, "write"); |
| 428 | return; |
| 429 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 430 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 431 | if (!tcp_flush(tcp, &error)) { |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 432 | if (grpc_tcp_trace) { |
| 433 | gpr_log(GPR_DEBUG, "write: delayed"); |
| 434 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); |
| 436 | } else { |
| 437 | cb = tcp->write_cb; |
| 438 | tcp->write_cb = NULL; |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 439 | if (grpc_tcp_trace) { |
| 440 | const char *str = grpc_error_string(error); |
| 441 | gpr_log(GPR_DEBUG, "write: %s", str); |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 442 | } |
Craig Tiller | e7603b8 | 2016-07-18 15:43:42 -0700 | [diff] [blame] | 443 | |
Craig Tiller | de2c41c | 2016-09-01 15:08:08 -0700 | [diff] [blame] | 444 | grpc_closure_run(exec_ctx, cb, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 445 | TCP_UNREF(exec_ctx, tcp, "write"); |
| 446 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 449 | static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 450 | grpc_slice_buffer *buf, grpc_closure *cb) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 451 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 452 | grpc_error *error = GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 453 | |
Craig Tiller | 44f6245 | 2016-06-23 08:22:30 -0700 | [diff] [blame] | 454 | if (grpc_tcp_trace) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 455 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 456 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 457 | for (i = 0; i < buf->count; i++) { |
| 458 | char *data = |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 459 | grpc_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); |
Mark D. Roth | bc84672 | 2016-05-04 10:53:50 -0700 | [diff] [blame] | 460 | gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | gpr_free(data); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 462 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 463 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 464 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 465 | GPR_TIMER_BEGIN("tcp_write", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 466 | GPR_ASSERT(tcp->write_cb == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 467 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 468 | if (buf->length == 0) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 469 | GPR_TIMER_END("tcp_write", 0); |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 470 | grpc_closure_sched( |
| 471 | exec_ctx, cb, |
| 472 | grpc_fd_is_shutdown(tcp->em_fd) |
| 473 | ? tcp_annotate_error(GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF"), |
| 474 | tcp) |
| 475 | : GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 476 | return; |
| 477 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 478 | tcp->outgoing_buffer = buf; |
| 479 | tcp->outgoing_slice_idx = 0; |
| 480 | tcp->outgoing_byte_idx = 0; |
| 481 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 482 | if (!tcp_flush(tcp, &error)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | TCP_REF(tcp, "write"); |
| 484 | tcp->write_cb = cb; |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 485 | if (grpc_tcp_trace) { |
| 486 | gpr_log(GPR_DEBUG, "write: delayed"); |
| 487 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 488 | grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); |
| 489 | } else { |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 490 | if (grpc_tcp_trace) { |
| 491 | const char *str = grpc_error_string(error); |
| 492 | gpr_log(GPR_DEBUG, "write: %s", str); |
Craig Tiller | c3df7b4 | 2016-07-18 15:51:26 -0700 | [diff] [blame] | 493 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 494 | grpc_closure_sched(exec_ctx, cb, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 495 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 496 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 497 | GPR_TIMER_END("tcp_write", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 500 | static void tcp_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 501 | grpc_pollset *pollset) { |
| 502 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 503 | grpc_pollset_add_fd(exec_ctx, pollset, tcp->em_fd); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 506 | static void tcp_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 507 | grpc_pollset_set *pollset_set) { |
| 508 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 509 | grpc_pollset_set_add_fd(exec_ctx, pollset_set, tcp->em_fd); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 512 | static char *tcp_get_peer(grpc_endpoint *ep) { |
| 513 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 514 | return gpr_strdup(tcp->peer_string); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 515 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 516 | |
Yuchen Zeng | 68413c2 | 2016-11-02 11:57:37 -0700 | [diff] [blame] | 517 | static int tcp_get_fd(grpc_endpoint *ep) { |
Yuchen Zeng | e5ec9ac | 2016-10-24 14:43:12 -0700 | [diff] [blame] | 518 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Yuchen Zeng | 68413c2 | 2016-11-02 11:57:37 -0700 | [diff] [blame] | 519 | return tcp->fd; |
Yuchen Zeng | e5ec9ac | 2016-10-24 14:43:12 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Craig Tiller | 70bd483 | 2016-06-30 14:20:46 -0700 | [diff] [blame] | 522 | static grpc_workqueue *tcp_get_workqueue(grpc_endpoint *ep) { |
| 523 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 524 | return grpc_fd_get_workqueue(tcp->em_fd); |
| 525 | } |
| 526 | |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 527 | static grpc_resource_user *tcp_get_resource_user(grpc_endpoint *ep) { |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 528 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Craig Tiller | a947f1c | 2016-11-04 13:53:17 -0700 | [diff] [blame] | 529 | return tcp->resource_user; |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Craig Tiller | 70bd483 | 2016-06-30 14:20:46 -0700 | [diff] [blame] | 532 | static const grpc_endpoint_vtable vtable = {tcp_read, |
| 533 | tcp_write, |
| 534 | tcp_get_workqueue, |
| 535 | tcp_add_to_pollset, |
| 536 | tcp_add_to_pollset_set, |
| 537 | tcp_shutdown, |
| 538 | tcp_destroy, |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 539 | tcp_get_resource_user, |
Yuchen Zeng | e5ec9ac | 2016-10-24 14:43:12 -0700 | [diff] [blame] | 540 | tcp_get_peer, |
Yuchen Zeng | 68413c2 | 2016-11-02 11:57:37 -0700 | [diff] [blame] | 541 | tcp_get_fd}; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 542 | |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 543 | grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, |
| 544 | grpc_resource_quota *resource_quota, |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 545 | size_t slice_size, const char *peer_string) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 546 | grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 547 | tcp->base.vtable = &vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 548 | tcp->peer_string = gpr_strdup(peer_string); |
Craig Tiller | 0a8a017 | 2016-02-25 07:36:27 -0800 | [diff] [blame] | 549 | tcp->fd = grpc_fd_wrapped_fd(em_fd); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 550 | tcp->read_cb = NULL; |
| 551 | tcp->write_cb = NULL; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 552 | tcp->release_fd_cb = NULL; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 553 | tcp->release_fd = NULL; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 554 | tcp->incoming_buffer = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 555 | tcp->slice_size = slice_size; |
Craig Tiller | 2b1fd66 | 2015-04-28 07:33:37 -0700 | [diff] [blame] | 556 | tcp->iov_size = 1; |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 557 | tcp->finished_edge = true; |
Craig Tiller | a947f1c | 2016-11-04 13:53:17 -0700 | [diff] [blame] | 558 | /* paired with unref in grpc_tcp_destroy */ |
| 559 | gpr_ref_init(&tcp->refcount, 1); |
Craig Tiller | e9f385a | 2016-09-26 14:54:43 -0700 | [diff] [blame] | 560 | gpr_atm_no_barrier_store(&tcp->shutdown_count, 0); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 561 | tcp->em_fd = em_fd; |
Craig Tiller | 3cb3447 | 2016-12-28 16:11:38 -0800 | [diff] [blame] | 562 | grpc_closure_init(&tcp->read_closure, tcp_handle_read, tcp, |
| 563 | grpc_schedule_on_exec_ctx); |
| 564 | grpc_closure_init(&tcp->write_closure, tcp_handle_write, tcp, |
| 565 | grpc_schedule_on_exec_ctx); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 566 | grpc_slice_buffer_init(&tcp->last_read_buffer); |
Craig Tiller | a947f1c | 2016-11-04 13:53:17 -0700 | [diff] [blame] | 567 | tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string); |
| 568 | grpc_resource_user_slice_allocator_init( |
| 569 | &tcp->slice_allocator, tcp->resource_user, tcp_read_allocation_done, tcp); |
Makarand Dharmapurikar | 0579cfc | 2016-06-20 15:45:24 -0700 | [diff] [blame] | 570 | /* Tell network status tracker about new endpoint */ |
| 571 | grpc_network_status_register_endpoint(&tcp->base); |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 572 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 573 | return &tcp->base; |
| 574 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 575 | |
Dan Born | 43a7803 | 2016-01-05 17:17:45 -0800 | [diff] [blame] | 576 | int grpc_tcp_fd(grpc_endpoint *ep) { |
| 577 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 578 | GPR_ASSERT(ep->vtable == &vtable); |
| 579 | return grpc_fd_wrapped_fd(tcp->em_fd); |
| 580 | } |
| 581 | |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 582 | 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] | 583 | int *fd, grpc_closure *done) { |
Craig Tiller | ce2ff3c | 2016-09-26 15:34:20 -0700 | [diff] [blame] | 584 | grpc_network_status_unregister_endpoint(ep); |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 585 | grpc_tcp *tcp = (grpc_tcp *)ep; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 586 | GPR_ASSERT(ep->vtable == &vtable); |
| 587 | tcp->release_fd = fd; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 588 | tcp->release_fd_cb = done; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 589 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, &tcp->last_read_buffer); |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 590 | TCP_UNREF(exec_ctx, tcp, "destroy"); |
| 591 | } |
| 592 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 593 | #endif |