Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
| 34 | #include "src/core/transport/chttp2_transport.h" |
| 35 | |
| 36 | #include <math.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/slice_buffer.h> |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 43 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 46 | #include "src/core/profiling/timers.h" |
| 47 | #include "src/core/support/string.h" |
| 48 | #include "src/core/transport/chttp2/http2_errors.h" |
| 49 | #include "src/core/transport/chttp2/internal.h" |
| 50 | #include "src/core/transport/chttp2/status_conversion.h" |
| 51 | #include "src/core/transport/chttp2/timeout_encoding.h" |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 52 | #include "src/core/transport/static_metadata.h" |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 53 | #include "src/core/transport/transport_impl.h" |
| 54 | |
ctiller | 493fbcc | 2014-12-07 15:09:10 -0800 | [diff] [blame] | 55 | #define DEFAULT_WINDOW 65535 |
| 56 | #define DEFAULT_CONNECTION_WINDOW_TARGET (1024 * 1024) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | #define MAX_WINDOW 0x7fffffffu |
| 58 | |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 59 | #define MAX_CLIENT_STREAM_ID 0x7fffffffu |
| 60 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 61 | int grpc_http_trace = 0; |
Craig Tiller | 6a8c038 | 2015-04-28 10:01:22 -0700 | [diff] [blame] | 62 | int grpc_flowctl_trace = 0; |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 63 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 64 | #define TRANSPORT_FROM_WRITING(tw) \ |
| 65 | ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ |
| 66 | writing))) |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 67 | |
Craig Tiller | cfb5db9 | 2015-06-15 17:14:41 -0700 | [diff] [blame] | 68 | #define TRANSPORT_FROM_PARSING(tw) \ |
| 69 | ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ |
| 70 | parsing))) |
| 71 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 72 | #define TRANSPORT_FROM_GLOBAL(tg) \ |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 73 | ((grpc_chttp2_transport *)((char *)(tg)-offsetof(grpc_chttp2_transport, \ |
| 74 | global))) |
| 75 | |
| 76 | #define STREAM_FROM_GLOBAL(sg) \ |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 77 | ((grpc_chttp2_stream *)((char *)(sg)-offsetof(grpc_chttp2_stream, global))) |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 78 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 79 | #define STREAM_FROM_PARSING(sg) \ |
| 80 | ((grpc_chttp2_stream *)((char *)(sg)-offsetof(grpc_chttp2_stream, parsing))) |
| 81 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | static const grpc_transport_vtable vtable; |
| 83 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | static void lock(grpc_chttp2_transport *t); |
| 85 | static void unlock(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 87 | /* forward declarations of various callbacks that we'll build closures around */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | static void writing_action(grpc_exec_ctx *exec_ctx, void *t, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 89 | bool iomgr_success_ignored); |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 90 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 91 | /** Set a transport level setting, and push it to our peer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 93 | uint32_t value); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 95 | /** Endpoint callback to process incoming data */ |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 96 | static void recv_data(grpc_exec_ctx *exec_ctx, void *tp, bool success); |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 97 | |
| 98 | /** Start disconnection chain */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | static void drop_connection(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t); |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 100 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 101 | /** Perform a transport_op */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static void perform_stream_op_locked( |
| 103 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global, |
| 104 | grpc_chttp2_stream_global *stream_global, grpc_transport_stream_op *op); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 105 | |
| 106 | /** Cancel a stream: coming from the transport API */ |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 107 | static void cancel_from_api(grpc_exec_ctx *exec_ctx, |
| 108 | grpc_chttp2_transport_global *transport_global, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | grpc_chttp2_stream_global *stream_global, |
| 110 | grpc_status_code status); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 111 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 112 | static void close_from_api(grpc_exec_ctx *exec_ctx, |
| 113 | grpc_chttp2_transport_global *transport_global, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | grpc_chttp2_stream_global *stream_global, |
| 115 | grpc_status_code status, |
| 116 | gpr_slice *optional_message); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 117 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 118 | /** Add endpoint from this transport to pollset */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | static void add_to_pollset_locked(grpc_exec_ctx *exec_ctx, |
| 120 | grpc_chttp2_transport *t, |
| 121 | grpc_pollset *pollset); |
| 122 | static void add_to_pollset_set_locked(grpc_exec_ctx *exec_ctx, |
| 123 | grpc_chttp2_transport *t, |
| 124 | grpc_pollset_set *pollset_set); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 125 | |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 126 | /** Start new streams that have been created if we can */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | static void maybe_start_some_streams( |
| 128 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global); |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 129 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | static void connectivity_state_set( |
| 131 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global, |
| 132 | grpc_connectivity_state state, const char *reason); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 133 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 134 | static void check_read_ops(grpc_exec_ctx *exec_ctx, |
| 135 | grpc_chttp2_transport_global *transport_global); |
| 136 | |
Craig Tiller | c8b7013 | 2015-12-09 20:49:09 -0800 | [diff] [blame] | 137 | static void incoming_byte_stream_update_flow_control( |
| 138 | grpc_chttp2_transport_global *transport_global, |
| 139 | grpc_chttp2_stream_global *stream_global, size_t max_size_hint, |
| 140 | size_t have_already); |
| 141 | |
Craig Tiller | a3f298f | 2015-12-16 19:42:09 -0800 | [diff] [blame] | 142 | static void fail_pending_writes(grpc_exec_ctx *exec_ctx, |
Craig Tiller | abb2e3d | 2015-12-15 06:23:59 -0800 | [diff] [blame] | 143 | grpc_chttp2_stream_global *stream_global); |
| 144 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 145 | /******************************************************************************* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | * CONSTRUCTION/DESTRUCTION/REFCOUNTING |
| 147 | */ |
| 148 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | static void destruct_transport(grpc_exec_ctx *exec_ctx, |
| 150 | grpc_chttp2_transport *t) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | size_t i; |
| 152 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | gpr_mu_lock(&t->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | GPR_ASSERT(t->ep == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | gpr_slice_buffer_destroy(&t->global.qbuf); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 158 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | gpr_slice_buffer_destroy(&t->writing.outbuf); |
| 160 | grpc_chttp2_hpack_compressor_destroy(&t->writing.hpack_compressor); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 161 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | gpr_slice_buffer_destroy(&t->parsing.qbuf); |
| 163 | gpr_slice_buffer_destroy(&t->read_buffer); |
| 164 | grpc_chttp2_hpack_parser_destroy(&t->parsing.hpack_parser); |
| 165 | grpc_chttp2_goaway_parser_destroy(&t->parsing.goaway_parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | for (i = 0; i < STREAM_LIST_COUNT; i++) { |
| 168 | GPR_ASSERT(t->lists[i].head == NULL); |
| 169 | GPR_ASSERT(t->lists[i].tail == NULL); |
| 170 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 171 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | GPR_ASSERT(grpc_chttp2_stream_map_size(&t->parsing_stream_map) == 0); |
| 173 | GPR_ASSERT(grpc_chttp2_stream_map_size(&t->new_stream_map) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 175 | grpc_chttp2_stream_map_destroy(&t->parsing_stream_map); |
| 176 | grpc_chttp2_stream_map_destroy(&t->new_stream_map); |
| 177 | grpc_connectivity_state_destroy(exec_ctx, &t->channel_callback.state_tracker); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | gpr_mu_unlock(&t->mu); |
| 180 | gpr_mu_destroy(&t->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | |
| 182 | /* callback remaining pings: they're not allowed to call into the transpot, |
| 183 | and maybe they hold resources that need to be freed */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | while (t->global.pings.next != &t->global.pings) { |
| 185 | grpc_chttp2_outstanding_ping *ping = t->global.pings.next; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 186 | grpc_exec_ctx_enqueue(exec_ctx, ping->on_recv, false, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | ping->next->prev = ping->prev; |
| 188 | ping->prev->next = ping->next; |
| 189 | gpr_free(ping); |
| 190 | } |
Craig Tiller | 8ed35ea | 2015-01-30 11:27:43 -0800 | [diff] [blame] | 191 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | gpr_free(t->peer_string); |
| 193 | gpr_free(t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 196 | #ifdef REFCOUNTING_DEBUG |
| 197 | #define REF_TRANSPORT(t, r) ref_transport(t, r, __FILE__, __LINE__) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 198 | #define UNREF_TRANSPORT(cl, t, r) unref_transport(cl, t, r, __FILE__, __LINE__) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | static void unref_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t, |
| 200 | const char *reason, const char *file, int line) { |
| 201 | gpr_log(GPR_DEBUG, "chttp2:unref:%p %d->%d %s [%s:%d]", t, t->refs.count, |
| 202 | t->refs.count - 1, reason, file, line); |
| 203 | if (!gpr_unref(&t->refs)) return; |
| 204 | destruct_transport(exec_ctx, t); |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 207 | static void ref_transport(grpc_chttp2_transport *t, const char *reason, |
| 208 | const char *file, int line) { |
| 209 | gpr_log(GPR_DEBUG, "chttp2: ref:%p %d->%d %s [%s:%d]", t, t->refs.count, |
| 210 | t->refs.count + 1, reason, file, line); |
| 211 | gpr_ref(&t->refs); |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 212 | } |
| 213 | #else |
| 214 | #define REF_TRANSPORT(t, r) ref_transport(t) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 215 | #define UNREF_TRANSPORT(cl, t, r) unref_transport(cl, t) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | static void unref_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) { |
| 217 | if (!gpr_unref(&t->refs)) return; |
| 218 | destruct_transport(exec_ctx, t); |
Craig Tiller | 9be83ee | 2015-02-18 14:16:15 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 221 | static void ref_transport(grpc_chttp2_transport *t) { gpr_ref(&t->refs); } |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 222 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 223 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 224 | static void init_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t, |
| 225 | const grpc_channel_args *channel_args, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 226 | grpc_endpoint *ep, uint8_t is_client) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 227 | size_t i; |
| 228 | int j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 229 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | GPR_ASSERT(strlen(GRPC_CHTTP2_CLIENT_CONNECT_STRING) == |
| 231 | GRPC_CHTTP2_CLIENT_CONNECT_STRLEN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 233 | memset(t, 0, sizeof(*t)); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 234 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 235 | t->base.vtable = &vtable; |
| 236 | t->ep = ep; |
| 237 | /* one ref is for destroy, the other for when ep becomes NULL */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 238 | gpr_ref_init(&t->refs, 2); |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 239 | /* ref is dropped at transport close() */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | gpr_ref_init(&t->shutdown_ep_refs, 1); |
| 241 | gpr_mu_init(&t->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | t->peer_string = grpc_endpoint_get_peer(ep); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 243 | t->endpoint_reading = 1; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 244 | t->global.next_stream_id = is_client ? 1 : 2; |
| 245 | t->global.is_client = is_client; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 246 | t->writing.outgoing_window = DEFAULT_WINDOW; |
| 247 | t->parsing.incoming_window = DEFAULT_WINDOW; |
Craig Tiller | a8d6809 | 2015-11-17 08:02:29 -0800 | [diff] [blame] | 248 | t->global.stream_lookahead = DEFAULT_WINDOW; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 249 | t->global.connection_window_target = DEFAULT_CONNECTION_WINDOW_TARGET; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 250 | t->global.ping_counter = 1; |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 251 | t->global.pings.next = t->global.pings.prev = &t->global.pings; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 252 | t->parsing.is_client = is_client; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | t->parsing.deframe_state = |
| 254 | is_client ? GRPC_DTS_FH_0 : GRPC_DTS_CLIENT_PREFIX_0; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 255 | t->writing.is_client = is_client; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 256 | grpc_connectivity_state_init( |
| 257 | &t->channel_callback.state_tracker, GRPC_CHANNEL_READY, |
| 258 | is_client ? "client_transport" : "server_transport"); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 259 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | gpr_slice_buffer_init(&t->global.qbuf); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 261 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 262 | gpr_slice_buffer_init(&t->writing.outbuf); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 263 | grpc_chttp2_hpack_compressor_init(&t->writing.hpack_compressor); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | grpc_closure_init(&t->writing_action, writing_action, t); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 265 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | gpr_slice_buffer_init(&t->parsing.qbuf); |
| 267 | grpc_chttp2_goaway_parser_init(&t->parsing.goaway_parser); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 268 | grpc_chttp2_hpack_parser_init(&t->parsing.hpack_parser); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 269 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 270 | grpc_closure_init(&t->writing.done_cb, grpc_chttp2_terminate_writing, |
| 271 | &t->writing); |
| 272 | grpc_closure_init(&t->recv_data, recv_data, t); |
| 273 | gpr_slice_buffer_init(&t->read_buffer); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 274 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | if (is_client) { |
| 276 | gpr_slice_buffer_add( |
| 277 | &t->global.qbuf, |
| 278 | gpr_slice_from_copied_string(GRPC_CHTTP2_CLIENT_CONNECT_STRING)); |
| 279 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 280 | /* 8 is a random stab in the dark as to a good initial size: it's small enough |
| 281 | that it shouldn't waste memory for infrequently used connections, yet |
| 282 | large enough that the exponential growth should happen nicely when it's |
| 283 | needed. |
| 284 | TODO(ctiller): tune this */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 285 | grpc_chttp2_stream_map_init(&t->parsing_stream_map, 8); |
| 286 | grpc_chttp2_stream_map_init(&t->new_stream_map, 8); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | |
| 288 | /* copy in initial settings to all setting sets */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | for (i = 0; i < GRPC_CHTTP2_NUM_SETTINGS; i++) { |
| 290 | t->parsing.settings[i] = grpc_chttp2_settings_parameters[i].default_value; |
| 291 | for (j = 0; j < GRPC_NUM_SETTING_SETS; j++) { |
| 292 | t->global.settings[j][i] = |
| 293 | grpc_chttp2_settings_parameters[i].default_value; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 294 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 295 | } |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 296 | t->global.dirtied_local_settings = 1; |
ctiller | 493fbcc | 2014-12-07 15:09:10 -0800 | [diff] [blame] | 297 | /* Hack: it's common for implementations to assume 65536 bytes initial send |
| 298 | window -- this should by rights be 0 */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 299 | t->global.force_send_settings = 1 << GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; |
| 300 | t->global.sent_local_settings = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 301 | |
| 302 | /* configure http2 the way we like it */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 303 | if (is_client) { |
| 304 | push_setting(t, GRPC_CHTTP2_SETTINGS_ENABLE_PUSH, 0); |
| 305 | push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 0); |
| 306 | } |
| 307 | push_setting(t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, DEFAULT_WINDOW); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 308 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 309 | if (channel_args) { |
| 310 | for (i = 0; i < channel_args->num_args; i++) { |
| 311 | if (0 == |
| 312 | strcmp(channel_args->args[i].key, GRPC_ARG_MAX_CONCURRENT_STREAMS)) { |
| 313 | if (is_client) { |
| 314 | gpr_log(GPR_ERROR, "%s: is ignored on the client", |
| 315 | GRPC_ARG_MAX_CONCURRENT_STREAMS); |
| 316 | } else if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
| 317 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 318 | GRPC_ARG_MAX_CONCURRENT_STREAMS); |
| 319 | } else { |
| 320 | push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 321 | (uint32_t)channel_args->args[i].value.integer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 322 | } |
| 323 | } else if (0 == strcmp(channel_args->args[i].key, |
| 324 | GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER)) { |
| 325 | if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
| 326 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 327 | GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER); |
| 328 | } else if ((t->global.next_stream_id & 1) != |
| 329 | (channel_args->args[i].value.integer & 1)) { |
| 330 | gpr_log(GPR_ERROR, "%s: low bit must be %d on %s", |
| 331 | GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, |
| 332 | t->global.next_stream_id & 1, |
| 333 | is_client ? "client" : "server"); |
| 334 | } else { |
| 335 | t->global.next_stream_id = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 336 | (uint32_t)channel_args->args[i].value.integer; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 337 | } |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 338 | } else if (0 == strcmp(channel_args->args[i].key, |
Craig Tiller | a8d6809 | 2015-11-17 08:02:29 -0800 | [diff] [blame] | 339 | GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES)) { |
| 340 | if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
| 341 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 342 | GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES); |
| 343 | } else if (channel_args->args[i].value.integer <= 5) { |
| 344 | gpr_log(GPR_ERROR, "%s: must be at least 5", |
| 345 | GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES); |
| 346 | } else { |
| 347 | t->global.stream_lookahead = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 348 | (uint32_t)channel_args->args[i].value.integer; |
Craig Tiller | a8d6809 | 2015-11-17 08:02:29 -0800 | [diff] [blame] | 349 | } |
Craig Tiller | a869b5b | 2015-11-23 16:28:57 -0800 | [diff] [blame] | 350 | } else if (0 == strcmp(channel_args->args[i].key, |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 351 | GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER)) { |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 352 | if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 353 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 354 | GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 355 | } else if (channel_args->args[i].value.integer < 0) { |
yang-g | 575625c | 2015-12-17 13:58:24 -0800 | [diff] [blame] | 356 | gpr_log(GPR_ERROR, "%s: must be non-negative", |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 357 | GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 358 | } else { |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 359 | push_setting(t, GRPC_CHTTP2_SETTINGS_HEADER_TABLE_SIZE, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 360 | (uint32_t)channel_args->args[i].value.integer); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 361 | } |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 362 | } else if (0 == strcmp(channel_args->args[i].key, |
| 363 | GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER)) { |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 364 | if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 365 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 366 | GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 367 | } else if (channel_args->args[i].value.integer < 0) { |
yang-g | 575625c | 2015-12-17 13:58:24 -0800 | [diff] [blame] | 368 | gpr_log(GPR_ERROR, "%s: must be non-negative", |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 369 | GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 370 | } else { |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 371 | grpc_chttp2_hpack_compressor_set_max_usable_size( |
| 372 | &t->writing.hpack_compressor, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 373 | (uint32_t)channel_args->args[i].value.integer); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 374 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 375 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 376 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 377 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 380 | static void destroy_transport(grpc_exec_ctx *exec_ctx, grpc_transport *gt) { |
| 381 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 382 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 383 | lock(t); |
Craig Tiller | 1fe7b9d | 2015-02-17 11:57:02 -0800 | [diff] [blame] | 384 | t->destroying = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 385 | drop_connection(exec_ctx, t); |
| 386 | unlock(exec_ctx, t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 387 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 388 | UNREF_TRANSPORT(exec_ctx, t, "destroy"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Craig Tiller | ae96a50 | 2015-09-01 15:15:42 -0700 | [diff] [blame] | 391 | /** block grpc_endpoint_shutdown being called until a paired |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 392 | allow_endpoint_shutdown is made */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | static void prevent_endpoint_shutdown(grpc_chttp2_transport *t) { |
| 394 | GPR_ASSERT(t->ep); |
| 395 | gpr_ref(&t->shutdown_ep_refs); |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 398 | static void allow_endpoint_shutdown_locked(grpc_exec_ctx *exec_ctx, |
| 399 | grpc_chttp2_transport *t) { |
| 400 | if (gpr_unref(&t->shutdown_ep_refs)) { |
| 401 | if (t->ep) { |
| 402 | grpc_endpoint_shutdown(exec_ctx, t->ep); |
Craig Tiller | b059ae5 | 2015-09-02 11:11:57 -0700 | [diff] [blame] | 403 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 404 | } |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 407 | static void allow_endpoint_shutdown_unlocked(grpc_exec_ctx *exec_ctx, |
| 408 | grpc_chttp2_transport *t) { |
| 409 | if (gpr_unref(&t->shutdown_ep_refs)) { |
| 410 | gpr_mu_lock(&t->mu); |
| 411 | if (t->ep) { |
| 412 | grpc_endpoint_shutdown(exec_ctx, t->ep); |
Craig Tiller | b059ae5 | 2015-09-02 11:11:57 -0700 | [diff] [blame] | 413 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 414 | gpr_mu_unlock(&t->mu); |
| 415 | } |
Craig Tiller | b059ae5 | 2015-09-02 11:11:57 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 418 | static void destroy_endpoint(grpc_exec_ctx *exec_ctx, |
| 419 | grpc_chttp2_transport *t) { |
| 420 | grpc_endpoint_destroy(exec_ctx, t->ep); |
Craig Tiller | b059ae5 | 2015-09-02 11:11:57 -0700 | [diff] [blame] | 421 | t->ep = NULL; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 422 | /* safe because we'll still have the ref for write */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 423 | UNREF_TRANSPORT(exec_ctx, t, "disconnect"); |
Craig Tiller | b059ae5 | 2015-09-02 11:11:57 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | static void close_transport_locked(grpc_exec_ctx *exec_ctx, |
| 427 | grpc_chttp2_transport *t) { |
| 428 | if (!t->closed) { |
| 429 | t->closed = 1; |
| 430 | connectivity_state_set(exec_ctx, &t->global, GRPC_CHANNEL_FATAL_FAILURE, |
| 431 | "close_transport"); |
| 432 | if (t->ep) { |
| 433 | allow_endpoint_shutdown_locked(exec_ctx, t); |
Craig Tiller | ec3aa89 | 2015-05-29 17:22:17 -0700 | [diff] [blame] | 434 | } |
Craig Tiller | ad23ae1 | 2016-03-04 10:50:56 -0800 | [diff] [blame] | 435 | |
| 436 | /* flush writable stream list to avoid dangling references */ |
| 437 | grpc_chttp2_stream_global *stream_global; |
| 438 | grpc_chttp2_stream_writing *stream_writing; |
| 439 | while (grpc_chttp2_list_pop_writable_stream( |
| 440 | &t->global, &t->writing, &stream_global, &stream_writing)) { |
| 441 | GRPC_CHTTP2_STREAM_UNREF(exec_ctx, stream_global, "chttp2_writing"); |
| 442 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 443 | } |
Craig Tiller | ec3aa89 | 2015-05-29 17:22:17 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 446 | #ifdef GRPC_STREAM_REFCOUNT_DEBUG |
| 447 | void grpc_chttp2_stream_ref(grpc_chttp2_stream_global *stream_global, |
| 448 | const char *reason) { |
| 449 | grpc_stream_ref(STREAM_FROM_GLOBAL(stream_global)->refcount, reason); |
| 450 | } |
| 451 | void grpc_chttp2_stream_unref(grpc_exec_ctx *exec_ctx, |
| 452 | grpc_chttp2_stream_global *stream_global, |
| 453 | const char *reason) { |
| 454 | grpc_stream_unref(exec_ctx, STREAM_FROM_GLOBAL(stream_global)->refcount, |
| 455 | reason); |
| 456 | } |
| 457 | #else |
| 458 | void grpc_chttp2_stream_ref(grpc_chttp2_stream_global *stream_global) { |
| 459 | grpc_stream_ref(STREAM_FROM_GLOBAL(stream_global)->refcount); |
| 460 | } |
| 461 | void grpc_chttp2_stream_unref(grpc_exec_ctx *exec_ctx, |
| 462 | grpc_chttp2_stream_global *stream_global) { |
| 463 | grpc_stream_unref(exec_ctx, STREAM_FROM_GLOBAL(stream_global)->refcount); |
| 464 | } |
| 465 | #endif |
| 466 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 467 | static int init_stream(grpc_exec_ctx *exec_ctx, grpc_transport *gt, |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 468 | grpc_stream *gs, grpc_stream_refcount *refcount, |
| 469 | const void *server_data) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 470 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 471 | grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 472 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 473 | memset(s, 0, sizeof(*s)); |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 474 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 475 | s->refcount = refcount; |
| 476 | GRPC_CHTTP2_STREAM_REF(&s->global, "chttp2"); |
| 477 | |
| 478 | grpc_chttp2_incoming_metadata_buffer_init(&s->parsing.metadata_buffer[0]); |
| 479 | grpc_chttp2_incoming_metadata_buffer_init(&s->parsing.metadata_buffer[1]); |
| 480 | grpc_chttp2_incoming_metadata_buffer_init( |
| 481 | &s->global.received_initial_metadata); |
| 482 | grpc_chttp2_incoming_metadata_buffer_init( |
| 483 | &s->global.received_trailing_metadata); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 484 | grpc_chttp2_data_parser_init(&s->parsing.data_parser); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 485 | gpr_slice_buffer_init(&s->writing.flow_controlled_buffer); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 486 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 487 | REF_TRANSPORT(t, "stream"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 488 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 489 | lock(t); |
| 490 | grpc_chttp2_register_stream(t, s); |
| 491 | if (server_data) { |
| 492 | GPR_ASSERT(t->parsing_active); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 493 | s->global.id = (uint32_t)(uintptr_t)server_data; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 494 | s->parsing.id = s->global.id; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 495 | s->global.outgoing_window = |
| 496 | t->global.settings[GRPC_PEER_SETTINGS] |
| 497 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 498 | s->parsing.incoming_window = s->global.max_recv_bytes = |
| 499 | t->global.settings[GRPC_SENT_SETTINGS] |
| 500 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 501 | *t->accepting_stream = s; |
| 502 | grpc_chttp2_stream_map_add(&t->parsing_stream_map, s->global.id, s); |
| 503 | s->global.in_stream_map = 1; |
| 504 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 505 | unlock(exec_ctx, t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 510 | static void destroy_stream(grpc_exec_ctx *exec_ctx, grpc_transport *gt, |
| 511 | grpc_stream *gs) { |
| 512 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 513 | grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; |
Craig Tiller | c3fdaf9 | 2015-06-18 10:16:52 -0700 | [diff] [blame] | 514 | int i; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 515 | grpc_byte_stream *bs; |
| 516 | |
| 517 | GPR_TIMER_BEGIN("destroy_stream", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 518 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 519 | gpr_mu_lock(&t->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 520 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 521 | GPR_ASSERT((s->global.write_closed && s->global.read_closed) || |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 522 | s->global.id == 0); |
| 523 | GPR_ASSERT(!s->global.in_stream_map); |
| 524 | if (grpc_chttp2_unregister_stream(t, s) && t->global.sent_goaway) { |
| 525 | close_transport_locked(exec_ctx, t); |
| 526 | } |
| 527 | if (!t->parsing_active && s->global.id) { |
| 528 | GPR_ASSERT(grpc_chttp2_stream_map_find(&t->parsing_stream_map, |
| 529 | s->global.id) == NULL); |
| 530 | } |
| 531 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 532 | grpc_chttp2_list_remove_unannounced_incoming_window_available(&t->global, |
| 533 | &s->global); |
yang-g | 0db9032 | 2015-12-21 13:38:56 -0800 | [diff] [blame] | 534 | grpc_chttp2_list_remove_stalled_by_transport(&t->global, &s->global); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 535 | |
| 536 | gpr_mu_unlock(&t->mu); |
| 537 | |
| 538 | for (i = 0; i < STREAM_LIST_COUNT; i++) { |
| 539 | if (s->included[i]) { |
| 540 | gpr_log(GPR_ERROR, "%s stream %d still included in list %d", |
| 541 | t->global.is_client ? "client" : "server", s->global.id, i); |
| 542 | abort(); |
Craig Tiller | 05cc0c4 | 2015-07-23 16:01:27 -0700 | [diff] [blame] | 543 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 544 | } |
Craig Tiller | c3fdaf9 | 2015-06-18 10:16:52 -0700 | [diff] [blame] | 545 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 546 | while ( |
| 547 | (bs = grpc_chttp2_incoming_frame_queue_pop(&s->global.incoming_frames))) { |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 548 | grpc_byte_stream_destroy(exec_ctx, bs); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | GPR_ASSERT(s->global.send_initial_metadata_finished == NULL); |
| 552 | GPR_ASSERT(s->global.send_message_finished == NULL); |
| 553 | GPR_ASSERT(s->global.send_trailing_metadata_finished == NULL); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 554 | GPR_ASSERT(s->global.recv_initial_metadata_ready == NULL); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 555 | GPR_ASSERT(s->global.recv_message_ready == NULL); |
| 556 | GPR_ASSERT(s->global.recv_trailing_metadata_finished == NULL); |
Craig Tiller | 7be556e | 2015-11-06 12:29:33 -0800 | [diff] [blame] | 557 | grpc_chttp2_data_parser_destroy(exec_ctx, &s->parsing.data_parser); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 558 | grpc_chttp2_incoming_metadata_buffer_destroy(&s->parsing.metadata_buffer[0]); |
| 559 | grpc_chttp2_incoming_metadata_buffer_destroy(&s->parsing.metadata_buffer[1]); |
| 560 | grpc_chttp2_incoming_metadata_buffer_destroy( |
| 561 | &s->global.received_initial_metadata); |
| 562 | grpc_chttp2_incoming_metadata_buffer_destroy( |
| 563 | &s->global.received_trailing_metadata); |
| 564 | gpr_slice_buffer_destroy(&s->writing.flow_controlled_buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 565 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 566 | UNREF_TRANSPORT(exec_ctx, t, "stream"); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 567 | |
| 568 | GPR_TIMER_END("destroy_stream", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 571 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 572 | grpc_chttp2_transport_parsing *transport_parsing, uint32_t id) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 573 | grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); |
| 574 | grpc_chttp2_stream *s = |
| 575 | grpc_chttp2_stream_map_find(&t->parsing_stream_map, id); |
Craig Tiller | cf1e319 | 2015-06-16 14:28:22 -0700 | [diff] [blame] | 576 | return s ? &s->parsing : NULL; |
Craig Tiller | cfb5db9 | 2015-06-15 17:14:41 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 579 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream( |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 580 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_parsing *transport_parsing, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 581 | uint32_t id) { |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 582 | grpc_chttp2_stream *accepting; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 583 | grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); |
| 584 | GPR_ASSERT(t->accepting_stream == NULL); |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 585 | t->accepting_stream = &accepting; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 586 | t->channel_callback.accept_stream(exec_ctx, |
| 587 | t->channel_callback.accept_stream_user_data, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 588 | &t->base, (void *)(uintptr_t)id); |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 589 | t->accepting_stream = NULL; |
| 590 | return &accepting->parsing; |
| 591 | } |
| 592 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 593 | /******************************************************************************* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 594 | * LOCK MANAGEMENT |
| 595 | */ |
| 596 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 597 | /* We take a grpc_chttp2_transport-global lock in response to calls coming in |
| 598 | from above, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 599 | and in response to data being received from below. New data to be written |
| 600 | is always queued, as are callbacks to process data. During unlock() we |
| 601 | check our todo lists and initiate callbacks and flush writes. */ |
| 602 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 603 | static void lock(grpc_chttp2_transport *t) { gpr_mu_lock(&t->mu); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 604 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 605 | static void unlock(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 606 | GPR_TIMER_BEGIN("unlock", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 607 | if (!t->writing_active && !t->closed && |
yang-g | 276e32d | 2016-02-22 13:15:30 -0800 | [diff] [blame] | 608 | grpc_chttp2_unlocking_check_writes(exec_ctx, &t->global, &t->writing, |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 609 | t->parsing_active)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 610 | t->writing_active = 1; |
| 611 | REF_TRANSPORT(t, "writing"); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 612 | grpc_exec_ctx_enqueue(exec_ctx, &t->writing_action, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 613 | prevent_endpoint_shutdown(t); |
| 614 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 615 | check_read_ops(exec_ctx, &t->global); |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 616 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 617 | gpr_mu_unlock(&t->mu); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 618 | GPR_TIMER_END("unlock", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 621 | /******************************************************************************* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 622 | * OUTPUT PROCESSING |
| 623 | */ |
| 624 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 625 | void grpc_chttp2_become_writable(grpc_chttp2_transport_global *transport_global, |
| 626 | grpc_chttp2_stream_global *stream_global) { |
| 627 | if (!TRANSPORT_FROM_GLOBAL(transport_global)->closed && |
| 628 | grpc_chttp2_list_add_writable_stream(transport_global, stream_global)) { |
| 629 | GRPC_CHTTP2_STREAM_REF(stream_global, "chttp2_writing"); |
| 630 | } |
| 631 | } |
| 632 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 633 | static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 634 | uint32_t value) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 635 | const grpc_chttp2_setting_parameters *sp = |
| 636 | &grpc_chttp2_settings_parameters[id]; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 637 | uint32_t use_value = GPR_CLAMP(value, sp->min_value, sp->max_value); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 638 | if (use_value != value) { |
| 639 | gpr_log(GPR_INFO, "Requested parameter %s clamped from %d to %d", sp->name, |
| 640 | value, use_value); |
| 641 | } |
| 642 | if (use_value != t->global.settings[GRPC_LOCAL_SETTINGS][id]) { |
| 643 | t->global.settings[GRPC_LOCAL_SETTINGS][id] = use_value; |
| 644 | t->global.dirtied_local_settings = 1; |
| 645 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 648 | void grpc_chttp2_terminate_writing(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 649 | void *transport_writing_ptr, bool success) { |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 650 | grpc_chttp2_transport_writing *transport_writing = transport_writing_ptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 651 | grpc_chttp2_transport *t = TRANSPORT_FROM_WRITING(transport_writing); |
Craig Tiller | abb2e3d | 2015-12-15 06:23:59 -0800 | [diff] [blame] | 652 | grpc_chttp2_stream_global *stream_global; |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 653 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 654 | GPR_TIMER_BEGIN("grpc_chttp2_terminate_writing", 0); |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 655 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 656 | lock(t); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 657 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 658 | allow_endpoint_shutdown_locked(exec_ctx, t); |
Craig Tiller | b059ae5 | 2015-09-02 11:11:57 -0700 | [diff] [blame] | 659 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 660 | if (!success) { |
| 661 | drop_connection(exec_ctx, t); |
| 662 | } |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 663 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 664 | grpc_chttp2_cleanup_writing(exec_ctx, &t->global, &t->writing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 665 | |
Craig Tiller | a3f298f | 2015-12-16 19:42:09 -0800 | [diff] [blame] | 666 | while (grpc_chttp2_list_pop_closed_waiting_for_writing(&t->global, |
| 667 | &stream_global)) { |
Craig Tiller | abb2e3d | 2015-12-15 06:23:59 -0800 | [diff] [blame] | 668 | fail_pending_writes(exec_ctx, stream_global); |
| 669 | GRPC_CHTTP2_STREAM_UNREF(exec_ctx, stream_global, "finish_writes"); |
| 670 | } |
| 671 | |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 672 | /* leave the writing flag up on shutdown to prevent further writes in unlock() |
| 673 | from starting */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 674 | t->writing_active = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 675 | if (t->ep && !t->endpoint_reading) { |
| 676 | destroy_endpoint(exec_ctx, t); |
| 677 | } |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 678 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 679 | unlock(exec_ctx, t); |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 680 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 681 | UNREF_TRANSPORT(exec_ctx, t, "writing"); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 682 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 683 | GPR_TIMER_END("grpc_chttp2_terminate_writing", 0); |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 686 | static void writing_action(grpc_exec_ctx *exec_ctx, void *gt, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 687 | bool iomgr_success_ignored) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 688 | grpc_chttp2_transport *t = gt; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 689 | GPR_TIMER_BEGIN("writing_action", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 690 | grpc_chttp2_perform_writes(exec_ctx, &t->writing, t->ep); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 691 | GPR_TIMER_END("writing_action", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 694 | void grpc_chttp2_add_incoming_goaway( |
| 695 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 696 | uint32_t goaway_error, gpr_slice goaway_text) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 697 | char *msg = gpr_dump_slice(goaway_text, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 698 | gpr_log(GPR_DEBUG, "got goaway [%d]: %s", goaway_error, msg); |
| 699 | gpr_free(msg); |
| 700 | gpr_slice_unref(goaway_text); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 701 | transport_global->seen_goaway = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 702 | connectivity_state_set(exec_ctx, transport_global, GRPC_CHANNEL_FATAL_FAILURE, |
| 703 | "got_goaway"); |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 706 | static void maybe_start_some_streams( |
| 707 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 708 | grpc_chttp2_stream_global *stream_global; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 709 | uint32_t stream_incoming_window; |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 710 | /* start streams where we have free grpc_chttp2_stream ids and free |
| 711 | * concurrency */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 712 | while (transport_global->next_stream_id <= MAX_CLIENT_STREAM_ID && |
| 713 | transport_global->concurrent_stream_count < |
| 714 | transport_global |
| 715 | ->settings[GRPC_PEER_SETTINGS] |
| 716 | [GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] && |
| 717 | grpc_chttp2_list_pop_waiting_for_concurrency(transport_global, |
| 718 | &stream_global)) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 719 | /* safe since we can't (legally) be parsing this stream yet */ |
| 720 | grpc_chttp2_stream_parsing *stream_parsing = |
| 721 | &STREAM_FROM_GLOBAL(stream_global)->parsing; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 722 | GRPC_CHTTP2_IF_TRACING(gpr_log( |
| 723 | GPR_DEBUG, "HTTP:%s: Allocating new grpc_chttp2_stream %p to id %d", |
| 724 | transport_global->is_client ? "CLI" : "SVR", stream_global, |
| 725 | transport_global->next_stream_id)); |
Craig Tiller | c1f7560 | 2015-04-24 11:44:53 -0700 | [diff] [blame] | 726 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 727 | GPR_ASSERT(stream_global->id == 0); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 728 | stream_global->id = stream_parsing->id = transport_global->next_stream_id; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 729 | transport_global->next_stream_id += 2; |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 730 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 731 | if (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID) { |
| 732 | connectivity_state_set(exec_ctx, transport_global, |
| 733 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 734 | "no_more_stream_ids"); |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 735 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 736 | |
| 737 | stream_global->outgoing_window = |
| 738 | transport_global->settings[GRPC_PEER_SETTINGS] |
| 739 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 740 | stream_parsing->incoming_window = stream_incoming_window = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 741 | transport_global->settings[GRPC_SENT_SETTINGS] |
| 742 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
| 743 | stream_global->max_recv_bytes = |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 744 | GPR_MAX(stream_incoming_window, stream_global->max_recv_bytes); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 745 | grpc_chttp2_stream_map_add( |
| 746 | &TRANSPORT_FROM_GLOBAL(transport_global)->new_stream_map, |
| 747 | stream_global->id, STREAM_FROM_GLOBAL(stream_global)); |
| 748 | stream_global->in_stream_map = 1; |
| 749 | transport_global->concurrent_stream_count++; |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 750 | grpc_chttp2_become_writable(transport_global, stream_global); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 751 | } |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 752 | /* cancel out streams that will never be started */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 753 | while (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID && |
| 754 | grpc_chttp2_list_pop_waiting_for_concurrency(transport_global, |
| 755 | &stream_global)) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 756 | cancel_from_api(exec_ctx, transport_global, stream_global, |
| 757 | GRPC_STATUS_UNAVAILABLE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 758 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 761 | static grpc_closure *add_closure_barrier(grpc_closure *closure) { |
| 762 | closure->final_data += 2; |
| 763 | return closure; |
| 764 | } |
| 765 | |
| 766 | void grpc_chttp2_complete_closure_step(grpc_exec_ctx *exec_ctx, |
| 767 | grpc_closure **pclosure, int success) { |
| 768 | grpc_closure *closure = *pclosure; |
| 769 | if (closure == NULL) { |
| 770 | return; |
| 771 | } |
| 772 | closure->final_data -= 2; |
| 773 | if (!success) { |
| 774 | closure->final_data |= 1; |
| 775 | } |
| 776 | if (closure->final_data < 2) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 777 | grpc_exec_ctx_enqueue(exec_ctx, closure, closure->final_data == 0, NULL); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 778 | } |
| 779 | *pclosure = NULL; |
| 780 | } |
| 781 | |
| 782 | static int contains_non_ok_status( |
| 783 | grpc_chttp2_transport_global *transport_global, |
| 784 | grpc_metadata_batch *batch) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 785 | grpc_linked_mdelem *l; |
| 786 | for (l = batch->list.head; l; l = l->next) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 787 | if (l->md->key == GRPC_MDSTR_GRPC_STATUS && |
| 788 | l->md != GRPC_MDELEM_GRPC_STATUS_0) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 789 | return 1; |
| 790 | } |
| 791 | } |
| 792 | return 0; |
| 793 | } |
| 794 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 795 | static void do_nothing(grpc_exec_ctx *exec_ctx, void *arg, bool success) {} |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 796 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 797 | static void perform_stream_op_locked( |
| 798 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global, |
| 799 | grpc_chttp2_stream_global *stream_global, grpc_transport_stream_op *op) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 800 | grpc_closure *on_complete; |
| 801 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 802 | GPR_TIMER_BEGIN("perform_stream_op_locked", 0); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 803 | |
| 804 | on_complete = op->on_complete; |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 805 | if (on_complete == NULL) { |
| 806 | on_complete = grpc_closure_create(do_nothing, NULL); |
| 807 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 808 | /* use final_data as a barrier until enqueue time; the inital counter is |
| 809 | dropped at the end of this function */ |
| 810 | on_complete->final_data = 2; |
| 811 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 812 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 813 | cancel_from_api(exec_ctx, transport_global, stream_global, |
| 814 | op->cancel_with_status); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 815 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 816 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 817 | if (op->close_with_status != GRPC_STATUS_OK) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 818 | close_from_api(exec_ctx, transport_global, stream_global, |
| 819 | op->close_with_status, op->optional_close_message); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 820 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 821 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 822 | if (op->send_initial_metadata != NULL) { |
| 823 | GPR_ASSERT(stream_global->send_initial_metadata_finished == NULL); |
| 824 | stream_global->send_initial_metadata_finished = |
| 825 | add_closure_barrier(on_complete); |
| 826 | stream_global->send_initial_metadata = op->send_initial_metadata; |
| 827 | if (contains_non_ok_status(transport_global, op->send_initial_metadata)) { |
| 828 | stream_global->seen_error = 1; |
| 829 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 830 | } |
| 831 | if (!stream_global->write_closed) { |
| 832 | if (transport_global->is_client) { |
| 833 | GPR_ASSERT(stream_global->id == 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 834 | grpc_chttp2_list_add_waiting_for_concurrency(transport_global, |
| 835 | stream_global); |
| 836 | maybe_start_some_streams(exec_ctx, transport_global); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 837 | } else { |
| 838 | GPR_ASSERT(stream_global->id != 0); |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 839 | grpc_chttp2_become_writable(transport_global, stream_global); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 840 | } |
| 841 | } else { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 842 | grpc_chttp2_complete_closure_step( |
| 843 | exec_ctx, &stream_global->send_initial_metadata_finished, 0); |
Craig Tiller | 5c517b1 | 2015-07-15 13:28:10 -0700 | [diff] [blame] | 844 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 845 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 846 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 847 | if (op->send_message != NULL) { |
| 848 | GPR_ASSERT(stream_global->send_message_finished == NULL); |
| 849 | GPR_ASSERT(stream_global->send_message == NULL); |
| 850 | stream_global->send_message_finished = add_closure_barrier(on_complete); |
| 851 | if (stream_global->write_closed) { |
| 852 | grpc_chttp2_complete_closure_step( |
| 853 | exec_ctx, &stream_global->send_message_finished, 0); |
Jan Tattermusch | fa45a5e | 2016-03-10 14:38:27 -0800 | [diff] [blame] | 854 | } else { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 855 | stream_global->send_message = op->send_message; |
Jan Tattermusch | fa45a5e | 2016-03-10 14:38:27 -0800 | [diff] [blame] | 856 | if (stream_global->id != 0) { |
| 857 | grpc_chttp2_become_writable(transport_global, stream_global); |
| 858 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 859 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 860 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 861 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 862 | if (op->send_trailing_metadata != NULL) { |
| 863 | GPR_ASSERT(stream_global->send_trailing_metadata_finished == NULL); |
| 864 | stream_global->send_trailing_metadata_finished = |
| 865 | add_closure_barrier(on_complete); |
| 866 | stream_global->send_trailing_metadata = op->send_trailing_metadata; |
| 867 | if (contains_non_ok_status(transport_global, op->send_trailing_metadata)) { |
| 868 | stream_global->seen_error = 1; |
| 869 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 870 | } |
| 871 | if (stream_global->write_closed) { |
| 872 | grpc_chttp2_complete_closure_step( |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 873 | exec_ctx, &stream_global->send_trailing_metadata_finished, |
Craig Tiller | e9c216e | 2015-11-06 14:48:46 -0800 | [diff] [blame] | 874 | grpc_metadata_batch_is_empty(op->send_trailing_metadata)); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 875 | } else if (stream_global->id != 0) { |
| 876 | /* TODO(ctiller): check if there's flow control for any outstanding |
| 877 | bytes before going writable */ |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 878 | grpc_chttp2_become_writable(transport_global, stream_global); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 879 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 882 | if (op->recv_initial_metadata != NULL) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 883 | GPR_ASSERT(stream_global->recv_initial_metadata_ready == NULL); |
| 884 | stream_global->recv_initial_metadata_ready = |
| 885 | op->recv_initial_metadata_ready; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 886 | stream_global->recv_initial_metadata = op->recv_initial_metadata; |
| 887 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 888 | } |
| 889 | |
| 890 | if (op->recv_message != NULL) { |
| 891 | GPR_ASSERT(stream_global->recv_message_ready == NULL); |
| 892 | stream_global->recv_message_ready = op->recv_message_ready; |
| 893 | stream_global->recv_message = op->recv_message; |
Craig Tiller | 8a09593 | 2015-12-16 19:40:49 -0800 | [diff] [blame] | 894 | if (stream_global->id != 0 && |
| 895 | (stream_global->incoming_frames.head == NULL || |
| 896 | stream_global->incoming_frames.head->is_tail)) { |
Craig Tiller | a3f298f | 2015-12-16 19:42:09 -0800 | [diff] [blame] | 897 | incoming_byte_stream_update_flow_control( |
| 898 | transport_global, stream_global, transport_global->stream_lookahead, |
| 899 | 0); |
Craig Tiller | c8b7013 | 2015-12-09 20:49:09 -0800 | [diff] [blame] | 900 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 901 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 902 | } |
| 903 | |
| 904 | if (op->recv_trailing_metadata != NULL) { |
| 905 | GPR_ASSERT(stream_global->recv_trailing_metadata_finished == NULL); |
| 906 | stream_global->recv_trailing_metadata_finished = |
| 907 | add_closure_barrier(on_complete); |
| 908 | stream_global->recv_trailing_metadata = op->recv_trailing_metadata; |
| 909 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 910 | } |
| 911 | |
| 912 | grpc_chttp2_complete_closure_step(exec_ctx, &on_complete, 1); |
| 913 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 914 | GPR_TIMER_END("perform_stream_op_locked", 0); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 915 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 916 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 917 | static void perform_stream_op(grpc_exec_ctx *exec_ctx, grpc_transport *gt, |
| 918 | grpc_stream *gs, grpc_transport_stream_op *op) { |
| 919 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 920 | grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 921 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 922 | lock(t); |
| 923 | perform_stream_op_locked(exec_ctx, &t->global, &s->global, op); |
| 924 | unlock(exec_ctx, t); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 927 | static void send_ping_locked(grpc_chttp2_transport *t, grpc_closure *on_recv) { |
| 928 | grpc_chttp2_outstanding_ping *p = gpr_malloc(sizeof(*p)); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 929 | p->next = &t->global.pings; |
| 930 | p->prev = p->next->prev; |
| 931 | p->prev->next = p->next->prev = p; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 932 | p->id[0] = (uint8_t)((t->global.ping_counter >> 56) & 0xff); |
| 933 | p->id[1] = (uint8_t)((t->global.ping_counter >> 48) & 0xff); |
| 934 | p->id[2] = (uint8_t)((t->global.ping_counter >> 40) & 0xff); |
| 935 | p->id[3] = (uint8_t)((t->global.ping_counter >> 32) & 0xff); |
| 936 | p->id[4] = (uint8_t)((t->global.ping_counter >> 24) & 0xff); |
| 937 | p->id[5] = (uint8_t)((t->global.ping_counter >> 16) & 0xff); |
| 938 | p->id[6] = (uint8_t)((t->global.ping_counter >> 8) & 0xff); |
| 939 | p->id[7] = (uint8_t)(t->global.ping_counter & 0xff); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 940 | p->on_recv = on_recv; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 941 | gpr_slice_buffer_add(&t->global.qbuf, grpc_chttp2_ping_create(0, p->id)); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 944 | void grpc_chttp2_ack_ping(grpc_exec_ctx *exec_ctx, |
| 945 | grpc_chttp2_transport_parsing *transport_parsing, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 946 | const uint8_t *opaque_8bytes) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 947 | grpc_chttp2_outstanding_ping *ping; |
| 948 | grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); |
| 949 | grpc_chttp2_transport_global *transport_global = &t->global; |
| 950 | lock(t); |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 951 | for (ping = transport_global->pings.next; ping != &transport_global->pings; |
| 952 | ping = ping->next) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 953 | if (0 == memcmp(opaque_8bytes, ping->id, 8)) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 954 | grpc_exec_ctx_enqueue(exec_ctx, ping->on_recv, true, NULL); |
Craig Tiller | 30798c0 | 2015-12-08 08:23:20 -0800 | [diff] [blame] | 955 | ping->next->prev = ping->prev; |
| 956 | ping->prev->next = ping->next; |
| 957 | gpr_free(ping); |
| 958 | break; |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 959 | } |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 960 | } |
| 961 | unlock(exec_ctx, t); |
| 962 | } |
| 963 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 964 | static void perform_transport_op_locked(grpc_exec_ctx *exec_ctx, |
| 965 | grpc_chttp2_transport *t, |
| 966 | grpc_transport_op *op) { |
| 967 | bool close_transport = false; |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 968 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 969 | grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, true, NULL); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 970 | |
Craig Tiller | 27e5aa4 | 2015-11-24 16:28:54 -0800 | [diff] [blame] | 971 | if (op->on_connectivity_state_change != NULL) { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 972 | grpc_connectivity_state_notify_on_state_change( |
| 973 | exec_ctx, &t->channel_callback.state_tracker, op->connectivity_state, |
| 974 | op->on_connectivity_state_change); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 975 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 976 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 977 | if (op->send_goaway) { |
| 978 | t->global.sent_goaway = 1; |
| 979 | grpc_chttp2_goaway_append( |
| 980 | t->global.last_incoming_stream_id, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 981 | (uint32_t)grpc_chttp2_grpc_status_to_http2_error(op->goaway_status), |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 982 | gpr_slice_ref(*op->goaway_message), &t->global.qbuf); |
| 983 | close_transport = !grpc_chttp2_has_streams(t); |
| 984 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 985 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 986 | if (op->set_accept_stream) { |
| 987 | t->channel_callback.accept_stream = op->set_accept_stream_fn; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 988 | t->channel_callback.accept_stream_user_data = |
| 989 | op->set_accept_stream_user_data; |
| 990 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 991 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 992 | if (op->bind_pollset) { |
| 993 | add_to_pollset_locked(exec_ctx, t, op->bind_pollset); |
| 994 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 995 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 996 | if (op->bind_pollset_set) { |
| 997 | add_to_pollset_set_locked(exec_ctx, t, op->bind_pollset_set); |
| 998 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 999 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1000 | if (op->send_ping) { |
| 1001 | send_ping_locked(t, op->send_ping); |
| 1002 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 1003 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1004 | if (op->disconnect) { |
| 1005 | close_transport_locked(exec_ctx, t); |
| 1006 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 1007 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1008 | if (close_transport) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1009 | close_transport_locked(exec_ctx, t); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1010 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 1013 | static void perform_transport_op(grpc_exec_ctx *exec_ctx, grpc_transport *gt, |
| 1014 | grpc_transport_op *op) { |
| 1015 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 1016 | |
| 1017 | lock(t); |
| 1018 | |
Craig Tiller | 1bd9ea4 | 2016-03-03 21:09:31 -0800 | [diff] [blame] | 1019 | /* If there's a set_accept_stream ensure that we're not parsing |
| 1020 | to avoid changing things out from underneath */ |
Craig Tiller | 687bf62 | 2016-03-03 21:18:48 -0800 | [diff] [blame] | 1021 | if (t->parsing_active && op->set_accept_stream) { |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 1022 | GPR_ASSERT(t->post_parsing_op == NULL); |
| 1023 | t->post_parsing_op = gpr_malloc(sizeof(*op)); |
| 1024 | memcpy(t->post_parsing_op, op, sizeof(*op)); |
| 1025 | } else { |
| 1026 | perform_transport_op_locked(exec_ctx, t, op); |
| 1027 | } |
| 1028 | |
| 1029 | unlock(exec_ctx, t); |
| 1030 | } |
| 1031 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1032 | /******************************************************************************* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1033 | * INPUT PROCESSING |
| 1034 | */ |
| 1035 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1036 | static void check_read_ops(grpc_exec_ctx *exec_ctx, |
| 1037 | grpc_chttp2_transport_global *transport_global) { |
| 1038 | grpc_chttp2_stream_global *stream_global; |
| 1039 | grpc_byte_stream *bs; |
| 1040 | while ( |
| 1041 | grpc_chttp2_list_pop_check_read_ops(transport_global, &stream_global)) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1042 | if (stream_global->recv_initial_metadata_ready != NULL && |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1043 | stream_global->published_initial_metadata) { |
| 1044 | grpc_chttp2_incoming_metadata_buffer_publish( |
| 1045 | &stream_global->received_initial_metadata, |
| 1046 | stream_global->recv_initial_metadata); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1047 | grpc_exec_ctx_enqueue( |
| 1048 | exec_ctx, stream_global->recv_initial_metadata_ready, true, NULL); |
| 1049 | stream_global->recv_initial_metadata_ready = NULL; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1050 | } |
| 1051 | if (stream_global->recv_message_ready != NULL) { |
yang-g | ad0df7b | 2016-02-22 10:00:20 -0800 | [diff] [blame] | 1052 | while (stream_global->seen_error && |
| 1053 | (bs = grpc_chttp2_incoming_frame_queue_pop( |
| 1054 | &stream_global->incoming_frames)) != NULL) { |
| 1055 | grpc_byte_stream_destroy(exec_ctx, bs); |
| 1056 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1057 | if (stream_global->incoming_frames.head != NULL) { |
| 1058 | *stream_global->recv_message = grpc_chttp2_incoming_frame_queue_pop( |
| 1059 | &stream_global->incoming_frames); |
| 1060 | GPR_ASSERT(*stream_global->recv_message != NULL); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 1061 | grpc_exec_ctx_enqueue(exec_ctx, stream_global->recv_message_ready, true, |
| 1062 | NULL); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1063 | stream_global->recv_message_ready = NULL; |
| 1064 | } else if (stream_global->published_trailing_metadata) { |
| 1065 | *stream_global->recv_message = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 1066 | grpc_exec_ctx_enqueue(exec_ctx, stream_global->recv_message_ready, true, |
| 1067 | NULL); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1068 | stream_global->recv_message_ready = NULL; |
| 1069 | } |
| 1070 | } |
| 1071 | if (stream_global->recv_trailing_metadata_finished != NULL && |
| 1072 | stream_global->read_closed && stream_global->write_closed) { |
| 1073 | while (stream_global->seen_error && |
| 1074 | (bs = grpc_chttp2_incoming_frame_queue_pop( |
| 1075 | &stream_global->incoming_frames)) != NULL) { |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 1076 | grpc_byte_stream_destroy(exec_ctx, bs); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1077 | } |
| 1078 | if (stream_global->incoming_frames.head == NULL) { |
| 1079 | grpc_chttp2_incoming_metadata_buffer_publish( |
| 1080 | &stream_global->received_trailing_metadata, |
| 1081 | stream_global->recv_trailing_metadata); |
| 1082 | grpc_chttp2_complete_closure_step( |
| 1083 | exec_ctx, &stream_global->recv_trailing_metadata_finished, 1); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
Craig Tiller | f73fcd1 | 2015-06-16 16:25:26 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1089 | static void remove_stream(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1090 | uint32_t id) { |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 1091 | size_t new_stream_count; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1092 | grpc_chttp2_stream *s = |
| 1093 | grpc_chttp2_stream_map_delete(&t->parsing_stream_map, id); |
| 1094 | if (!s) { |
| 1095 | s = grpc_chttp2_stream_map_delete(&t->new_stream_map, id); |
| 1096 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1097 | GPR_ASSERT(s); |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 1098 | s->global.in_stream_map = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1099 | if (t->parsing.incoming_stream == &s->parsing) { |
| 1100 | t->parsing.incoming_stream = NULL; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1101 | grpc_chttp2_parsing_become_skip_parser(exec_ctx, &t->parsing); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1102 | } |
Craig Tiller | 1a423ef | 2015-12-14 21:51:38 -0800 | [diff] [blame] | 1103 | if (s->parsing.data_parser.parsing_frame != NULL) { |
| 1104 | grpc_chttp2_incoming_byte_stream_finished( |
| 1105 | exec_ctx, s->parsing.data_parser.parsing_frame, 0, 0); |
| 1106 | s->parsing.data_parser.parsing_frame = NULL; |
| 1107 | } |
| 1108 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1109 | if (grpc_chttp2_unregister_stream(t, s) && t->global.sent_goaway) { |
| 1110 | close_transport_locked(exec_ctx, t); |
| 1111 | } |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1112 | if (grpc_chttp2_list_remove_writable_stream(&t->global, &s->global)) { |
| 1113 | GRPC_CHTTP2_STREAM_UNREF(exec_ctx, &s->global, "chttp2_writing"); |
| 1114 | } |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 1115 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1116 | new_stream_count = grpc_chttp2_stream_map_size(&t->parsing_stream_map) + |
| 1117 | grpc_chttp2_stream_map_size(&t->new_stream_map); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1118 | GPR_ASSERT(new_stream_count <= UINT32_MAX); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1119 | if (new_stream_count != t->global.concurrent_stream_count) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1120 | t->global.concurrent_stream_count = (uint32_t)new_stream_count; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1121 | maybe_start_some_streams(exec_ctx, &t->global); |
| 1122 | } |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1125 | static void cancel_from_api(grpc_exec_ctx *exec_ctx, |
| 1126 | grpc_chttp2_transport_global *transport_global, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1127 | grpc_chttp2_stream_global *stream_global, |
| 1128 | grpc_status_code status) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1129 | if (stream_global->id != 0) { |
| 1130 | gpr_slice_buffer_add( |
| 1131 | &transport_global->qbuf, |
| 1132 | grpc_chttp2_rst_stream_create( |
| 1133 | stream_global->id, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1134 | (uint32_t)grpc_chttp2_grpc_status_to_http2_error(status))); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1135 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1136 | grpc_chttp2_fake_status(exec_ctx, transport_global, stream_global, status, |
| 1137 | NULL); |
| 1138 | grpc_chttp2_mark_stream_closed(exec_ctx, transport_global, stream_global, 1, |
| 1139 | 1); |
Craig Tiller | b787c50 | 2015-06-15 16:14:52 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1142 | void grpc_chttp2_fake_status(grpc_exec_ctx *exec_ctx, |
| 1143 | grpc_chttp2_transport_global *transport_global, |
| 1144 | grpc_chttp2_stream_global *stream_global, |
| 1145 | grpc_status_code status, gpr_slice *slice) { |
| 1146 | if (status != GRPC_STATUS_OK) { |
| 1147 | stream_global->seen_error = 1; |
| 1148 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 1149 | } |
Craig Tiller | 4b65b1d | 2015-11-03 07:04:30 -0800 | [diff] [blame] | 1150 | /* stream_global->recv_trailing_metadata_finished gives us a |
| 1151 | last chance replacement: we've received trailing metadata, |
| 1152 | but something more important has become available to signal |
| 1153 | to the upper layers - drop what we've got, and then publish |
| 1154 | what we want - which is safe because we haven't told anyone |
| 1155 | about the metadata yet */ |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 1156 | if (!stream_global->published_trailing_metadata || |
| 1157 | stream_global->recv_trailing_metadata_finished != NULL) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1158 | char status_string[GPR_LTOA_MIN_BUFSIZE]; |
| 1159 | gpr_ltoa(status, status_string); |
| 1160 | grpc_chttp2_incoming_metadata_buffer_add( |
| 1161 | &stream_global->received_trailing_metadata, |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1162 | grpc_mdelem_from_metadata_strings( |
| 1163 | GRPC_MDSTR_GRPC_STATUS, grpc_mdstr_from_string(status_string))); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1164 | if (slice) { |
| 1165 | grpc_chttp2_incoming_metadata_buffer_add( |
| 1166 | &stream_global->received_trailing_metadata, |
| 1167 | grpc_mdelem_from_metadata_strings( |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1168 | GRPC_MDSTR_GRPC_MESSAGE, |
| 1169 | grpc_mdstr_from_slice(gpr_slice_ref(*slice)))); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1170 | } |
| 1171 | stream_global->published_trailing_metadata = 1; |
| 1172 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 1173 | } |
| 1174 | if (slice) { |
| 1175 | gpr_slice_unref(*slice); |
| 1176 | } |
| 1177 | } |
| 1178 | |
Craig Tiller | a3f298f | 2015-12-16 19:42:09 -0800 | [diff] [blame] | 1179 | static void fail_pending_writes(grpc_exec_ctx *exec_ctx, |
Craig Tiller | abb2e3d | 2015-12-15 06:23:59 -0800 | [diff] [blame] | 1180 | grpc_chttp2_stream_global *stream_global) { |
| 1181 | grpc_chttp2_complete_closure_step( |
| 1182 | exec_ctx, &stream_global->send_initial_metadata_finished, 0); |
| 1183 | grpc_chttp2_complete_closure_step( |
| 1184 | exec_ctx, &stream_global->send_trailing_metadata_finished, 0); |
| 1185 | grpc_chttp2_complete_closure_step(exec_ctx, |
| 1186 | &stream_global->send_message_finished, 0); |
| 1187 | } |
| 1188 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1189 | void grpc_chttp2_mark_stream_closed( |
| 1190 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global, |
| 1191 | grpc_chttp2_stream_global *stream_global, int close_reads, |
| 1192 | int close_writes) { |
| 1193 | if (stream_global->read_closed && stream_global->write_closed) { |
| 1194 | /* already closed */ |
| 1195 | return; |
| 1196 | } |
| 1197 | grpc_chttp2_list_add_check_read_ops(transport_global, stream_global); |
| 1198 | if (close_reads && !stream_global->read_closed) { |
| 1199 | stream_global->read_closed = 1; |
| 1200 | stream_global->published_initial_metadata = 1; |
| 1201 | stream_global->published_trailing_metadata = 1; |
| 1202 | } |
| 1203 | if (close_writes && !stream_global->write_closed) { |
| 1204 | stream_global->write_closed = 1; |
Craig Tiller | abb2e3d | 2015-12-15 06:23:59 -0800 | [diff] [blame] | 1205 | if (TRANSPORT_FROM_GLOBAL(transport_global)->writing_active) { |
| 1206 | GRPC_CHTTP2_STREAM_REF(stream_global, "finish_writes"); |
| 1207 | grpc_chttp2_list_add_closed_waiting_for_writing(transport_global, |
| 1208 | stream_global); |
| 1209 | } else { |
| 1210 | fail_pending_writes(exec_ctx, stream_global); |
| 1211 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1212 | } |
| 1213 | if (stream_global->read_closed && stream_global->write_closed) { |
| 1214 | if (stream_global->id != 0 && |
| 1215 | TRANSPORT_FROM_GLOBAL(transport_global)->parsing_active) { |
| 1216 | grpc_chttp2_list_add_closed_waiting_for_parsing(transport_global, |
| 1217 | stream_global); |
| 1218 | } else { |
| 1219 | if (stream_global->id != 0) { |
| 1220 | remove_stream(exec_ctx, TRANSPORT_FROM_GLOBAL(transport_global), |
| 1221 | stream_global->id); |
| 1222 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1223 | GRPC_CHTTP2_STREAM_UNREF(exec_ctx, stream_global, "chttp2"); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | static void close_from_api(grpc_exec_ctx *exec_ctx, |
| 1229 | grpc_chttp2_transport_global *transport_global, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1230 | grpc_chttp2_stream_global *stream_global, |
| 1231 | grpc_status_code status, |
| 1232 | gpr_slice *optional_message) { |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1233 | gpr_slice hdr; |
| 1234 | gpr_slice status_hdr; |
| 1235 | gpr_slice message_pfx; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1236 | uint8_t *p; |
| 1237 | uint32_t len = 0; |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1238 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1239 | GPR_ASSERT(status >= 0 && (int)status < 100); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1240 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1241 | GPR_ASSERT(stream_global->id != 0); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1242 | |
| 1243 | /* Hand roll a header block. |
| 1244 | This is unnecessarily ugly - at some point we should find a more elegant |
| 1245 | solution. |
| 1246 | It's complicated by the fact that our send machinery would be dead by the |
| 1247 | time we got around to sending this, so instead we ignore HPACK compression |
| 1248 | and just write the uncompressed bytes onto the wire. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1249 | status_hdr = gpr_slice_malloc(15 + (status >= 10)); |
| 1250 | p = GPR_SLICE_START_PTR(status_hdr); |
| 1251 | *p++ = 0x40; /* literal header */ |
| 1252 | *p++ = 11; /* len(grpc-status) */ |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1253 | *p++ = 'g'; |
| 1254 | *p++ = 'r'; |
| 1255 | *p++ = 'p'; |
| 1256 | *p++ = 'c'; |
| 1257 | *p++ = '-'; |
| 1258 | *p++ = 's'; |
| 1259 | *p++ = 't'; |
| 1260 | *p++ = 'a'; |
| 1261 | *p++ = 't'; |
| 1262 | *p++ = 'u'; |
| 1263 | *p++ = 's'; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1264 | if (status < 10) { |
| 1265 | *p++ = 1; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1266 | *p++ = (uint8_t)('0' + status); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1267 | } else { |
| 1268 | *p++ = 2; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1269 | *p++ = (uint8_t)('0' + (status / 10)); |
| 1270 | *p++ = (uint8_t)('0' + (status % 10)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1271 | } |
| 1272 | GPR_ASSERT(p == GPR_SLICE_END_PTR(status_hdr)); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1273 | len += (uint32_t)GPR_SLICE_LENGTH(status_hdr); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1274 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1275 | if (optional_message) { |
| 1276 | GPR_ASSERT(GPR_SLICE_LENGTH(*optional_message) < 127); |
| 1277 | message_pfx = gpr_slice_malloc(15); |
| 1278 | p = GPR_SLICE_START_PTR(message_pfx); |
| 1279 | *p++ = 0x40; |
| 1280 | *p++ = 12; /* len(grpc-message) */ |
| 1281 | *p++ = 'g'; |
| 1282 | *p++ = 'r'; |
| 1283 | *p++ = 'p'; |
| 1284 | *p++ = 'c'; |
| 1285 | *p++ = '-'; |
| 1286 | *p++ = 'm'; |
| 1287 | *p++ = 'e'; |
| 1288 | *p++ = 's'; |
| 1289 | *p++ = 's'; |
| 1290 | *p++ = 'a'; |
| 1291 | *p++ = 'g'; |
| 1292 | *p++ = 'e'; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1293 | *p++ = (uint8_t)GPR_SLICE_LENGTH(*optional_message); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1294 | GPR_ASSERT(p == GPR_SLICE_END_PTR(message_pfx)); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1295 | len += (uint32_t)GPR_SLICE_LENGTH(message_pfx); |
| 1296 | len += (uint32_t)GPR_SLICE_LENGTH(*optional_message); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1297 | } |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1298 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1299 | hdr = gpr_slice_malloc(9); |
| 1300 | p = GPR_SLICE_START_PTR(hdr); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1301 | *p++ = (uint8_t)(len >> 16); |
| 1302 | *p++ = (uint8_t)(len >> 8); |
| 1303 | *p++ = (uint8_t)(len); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1304 | *p++ = GRPC_CHTTP2_FRAME_HEADER; |
| 1305 | *p++ = GRPC_CHTTP2_DATA_FLAG_END_STREAM | GRPC_CHTTP2_DATA_FLAG_END_HEADERS; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1306 | *p++ = (uint8_t)(stream_global->id >> 24); |
| 1307 | *p++ = (uint8_t)(stream_global->id >> 16); |
| 1308 | *p++ = (uint8_t)(stream_global->id >> 8); |
| 1309 | *p++ = (uint8_t)(stream_global->id); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1310 | GPR_ASSERT(p == GPR_SLICE_END_PTR(hdr)); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1311 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1312 | gpr_slice_buffer_add(&transport_global->qbuf, hdr); |
| 1313 | gpr_slice_buffer_add(&transport_global->qbuf, status_hdr); |
| 1314 | if (optional_message) { |
| 1315 | gpr_slice_buffer_add(&transport_global->qbuf, message_pfx); |
| 1316 | gpr_slice_buffer_add(&transport_global->qbuf, |
| 1317 | gpr_slice_ref(*optional_message)); |
| 1318 | } |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1319 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1320 | gpr_slice_buffer_add( |
| 1321 | &transport_global->qbuf, |
| 1322 | grpc_chttp2_rst_stream_create(stream_global->id, GRPC_CHTTP2_NO_ERROR)); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1323 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1324 | if (optional_message) { |
| 1325 | gpr_slice_ref(*optional_message); |
| 1326 | } |
| 1327 | grpc_chttp2_fake_status(exec_ctx, transport_global, stream_global, status, |
| 1328 | optional_message); |
| 1329 | grpc_chttp2_mark_stream_closed(exec_ctx, transport_global, stream_global, 1, |
| 1330 | 1); |
Craig Tiller | 45ce927 | 2015-07-31 11:22:35 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1333 | static void cancel_stream_cb(grpc_chttp2_transport_global *transport_global, |
| 1334 | void *user_data, |
| 1335 | grpc_chttp2_stream_global *stream_global) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1336 | cancel_from_api(user_data, transport_global, stream_global, |
| 1337 | GRPC_STATUS_UNAVAILABLE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1340 | static void end_all_the_calls(grpc_exec_ctx *exec_ctx, |
| 1341 | grpc_chttp2_transport *t) { |
| 1342 | grpc_chttp2_for_all_streams(&t->global, exec_ctx, cancel_stream_cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1345 | static void drop_connection(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) { |
| 1346 | close_transport_locked(exec_ctx, t); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1347 | end_all_the_calls(exec_ctx, t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 1350 | /** update window from a settings change */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1351 | static void update_global_window(void *args, uint32_t id, void *stream) { |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 1352 | grpc_chttp2_transport *t = args; |
| 1353 | grpc_chttp2_stream *s = stream; |
| 1354 | grpc_chttp2_transport_global *transport_global = &t->global; |
| 1355 | grpc_chttp2_stream_global *stream_global = &s->global; |
Craig Tiller | 6f7411f | 2015-07-06 15:37:28 -0700 | [diff] [blame] | 1356 | int was_zero; |
| 1357 | int is_zero; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1358 | int64_t initial_window_update = t->parsing.initial_window_update; |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 1359 | |
Craig Tiller | 6f7411f | 2015-07-06 15:37:28 -0700 | [diff] [blame] | 1360 | was_zero = stream_global->outgoing_window <= 0; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1361 | GRPC_CHTTP2_FLOW_CREDIT_STREAM("settings", transport_global, stream_global, |
| 1362 | outgoing_window, initial_window_update); |
Craig Tiller | 6f7411f | 2015-07-06 15:37:28 -0700 | [diff] [blame] | 1363 | is_zero = stream_global->outgoing_window <= 0; |
| 1364 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1365 | if (was_zero && !is_zero) { |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1366 | grpc_chttp2_become_writable(transport_global, stream_global); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1367 | } |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1370 | static void read_error_locked(grpc_exec_ctx *exec_ctx, |
| 1371 | grpc_chttp2_transport *t) { |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 1372 | t->endpoint_reading = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1373 | if (!t->writing_active && t->ep) { |
| 1374 | destroy_endpoint(exec_ctx, t); |
| 1375 | } |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1378 | /* tcp read callback */ |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 1379 | static void recv_data(grpc_exec_ctx *exec_ctx, void *tp, bool success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1380 | size_t i; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 1381 | int keep_reading = 0; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1382 | grpc_chttp2_transport *t = tp; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1383 | grpc_chttp2_transport_global *transport_global = &t->global; |
| 1384 | grpc_chttp2_transport_parsing *transport_parsing = &t->parsing; |
| 1385 | grpc_chttp2_stream_global *stream_global; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1386 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1387 | GPR_TIMER_BEGIN("recv_data", 0); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 1388 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1389 | lock(t); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 1390 | i = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1391 | GPR_ASSERT(!t->parsing_active); |
| 1392 | if (!t->closed) { |
| 1393 | t->parsing_active = 1; |
| 1394 | /* merge stream lists */ |
| 1395 | grpc_chttp2_stream_map_move_into(&t->new_stream_map, |
| 1396 | &t->parsing_stream_map); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1397 | grpc_chttp2_prepare_to_read(transport_global, transport_parsing); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1398 | gpr_mu_unlock(&t->mu); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1399 | GPR_TIMER_BEGIN("recv_data.parse", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1400 | for (; i < t->read_buffer.count && |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1401 | grpc_chttp2_perform_read(exec_ctx, transport_parsing, |
| 1402 | t->read_buffer.slices[i]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1403 | i++) |
| 1404 | ; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1405 | GPR_TIMER_END("recv_data.parse", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1406 | gpr_mu_lock(&t->mu); |
Craig Tiller | 5c3a110 | 2015-12-02 16:13:24 -0800 | [diff] [blame] | 1407 | /* copy parsing qbuf to global qbuf */ |
| 1408 | gpr_slice_buffer_move_into(&t->parsing.qbuf, &t->global.qbuf); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1409 | if (i != t->read_buffer.count) { |
Craig Tiller | 5c3a110 | 2015-12-02 16:13:24 -0800 | [diff] [blame] | 1410 | unlock(exec_ctx, t); |
| 1411 | lock(t); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1412 | drop_connection(exec_ctx, t); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 1413 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1414 | /* merge stream lists */ |
| 1415 | grpc_chttp2_stream_map_move_into(&t->new_stream_map, |
| 1416 | &t->parsing_stream_map); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1417 | transport_global->concurrent_stream_count = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1418 | (uint32_t)grpc_chttp2_stream_map_size(&t->parsing_stream_map); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1419 | if (transport_parsing->initial_window_update != 0) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1420 | grpc_chttp2_stream_map_for_each(&t->parsing_stream_map, |
| 1421 | update_global_window, t); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1422 | transport_parsing->initial_window_update = 0; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 1423 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1424 | /* handle higher level things */ |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1425 | grpc_chttp2_publish_reads(exec_ctx, transport_global, transport_parsing); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1426 | t->parsing_active = 0; |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 1427 | /* handle delayed transport ops (if there is one) */ |
| 1428 | if (t->post_parsing_op) { |
| 1429 | grpc_transport_op *op = t->post_parsing_op; |
| 1430 | t->post_parsing_op = NULL; |
| 1431 | perform_transport_op_locked(exec_ctx, t, op); |
| 1432 | gpr_free(op); |
| 1433 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1434 | /* if a stream is in the stream map, and gets cancelled, we need to ensure |
| 1435 | * we are not parsing before continuing the cancellation to keep things in |
| 1436 | * a sane state */ |
| 1437 | while (grpc_chttp2_list_pop_closed_waiting_for_parsing(transport_global, |
| 1438 | &stream_global)) { |
| 1439 | GPR_ASSERT(stream_global->in_stream_map); |
| 1440 | GPR_ASSERT(stream_global->write_closed); |
| 1441 | GPR_ASSERT(stream_global->read_closed); |
| 1442 | remove_stream(exec_ctx, t, stream_global->id); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1443 | GRPC_CHTTP2_STREAM_UNREF(exec_ctx, stream_global, "chttp2"); |
| 1444 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1445 | } |
yang-g | 759f59a | 2015-10-02 10:38:22 -0700 | [diff] [blame] | 1446 | if (!success || i != t->read_buffer.count || t->closed) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1447 | drop_connection(exec_ctx, t); |
| 1448 | read_error_locked(exec_ctx, t); |
| 1449 | } else if (!t->closed) { |
| 1450 | keep_reading = 1; |
| 1451 | REF_TRANSPORT(t, "keep_reading"); |
| 1452 | prevent_endpoint_shutdown(t); |
| 1453 | } |
| 1454 | gpr_slice_buffer_reset_and_unref(&t->read_buffer); |
| 1455 | unlock(exec_ctx, t); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 1456 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1457 | if (keep_reading) { |
| 1458 | grpc_endpoint_read(exec_ctx, t->ep, &t->read_buffer, &t->recv_data); |
| 1459 | allow_endpoint_shutdown_unlocked(exec_ctx, t); |
| 1460 | UNREF_TRANSPORT(exec_ctx, t, "keep_reading"); |
| 1461 | } else { |
| 1462 | UNREF_TRANSPORT(exec_ctx, t, "recv_data"); |
| 1463 | } |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 1464 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1465 | GPR_TIMER_END("recv_data", 0); |
Craig Tiller | 8b2f1d7 | 2015-06-18 13:38:38 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1468 | /******************************************************************************* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1469 | * CALLBACK LOOP |
| 1470 | */ |
| 1471 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1472 | static void connectivity_state_set( |
| 1473 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global, |
| 1474 | grpc_connectivity_state state, const char *reason) { |
| 1475 | GRPC_CHTTP2_IF_TRACING( |
| 1476 | gpr_log(GPR_DEBUG, "set connectivity_state=%d", state)); |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1477 | grpc_connectivity_state_set( |
| 1478 | exec_ctx, |
| 1479 | &TRANSPORT_FROM_GLOBAL(transport_global)->channel_callback.state_tracker, |
| 1480 | state, reason); |
Craig Tiller | 748fe3f | 2015-03-02 07:48:50 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1483 | /******************************************************************************* |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 1484 | * POLLSET STUFF |
| 1485 | */ |
| 1486 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1487 | static void add_to_pollset_locked(grpc_exec_ctx *exec_ctx, |
| 1488 | grpc_chttp2_transport *t, |
| 1489 | grpc_pollset *pollset) { |
| 1490 | if (t->ep) { |
| 1491 | grpc_endpoint_add_to_pollset(exec_ctx, t->ep, pollset); |
| 1492 | } |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1495 | static void add_to_pollset_set_locked(grpc_exec_ctx *exec_ctx, |
| 1496 | grpc_chttp2_transport *t, |
| 1497 | grpc_pollset_set *pollset_set) { |
| 1498 | if (t->ep) { |
| 1499 | grpc_endpoint_add_to_pollset_set(exec_ctx, t->ep, pollset_set); |
| 1500 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1503 | static void set_pollset(grpc_exec_ctx *exec_ctx, grpc_transport *gt, |
| 1504 | grpc_stream *gs, grpc_pollset *pollset) { |
| 1505 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 1506 | lock(t); |
| 1507 | add_to_pollset_locked(exec_ctx, t, pollset); |
| 1508 | unlock(exec_ctx, t); |
| 1509 | } |
| 1510 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1511 | /******************************************************************************* |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1512 | * BYTE STREAM |
| 1513 | */ |
| 1514 | |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1515 | static void incoming_byte_stream_update_flow_control( |
| 1516 | grpc_chttp2_transport_global *transport_global, |
| 1517 | grpc_chttp2_stream_global *stream_global, size_t max_size_hint, |
| 1518 | size_t have_already) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1519 | uint32_t max_recv_bytes; |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1520 | |
| 1521 | /* clamp max recv hint to an allowable size */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1522 | if (max_size_hint >= UINT32_MAX - transport_global->stream_lookahead) { |
| 1523 | max_recv_bytes = UINT32_MAX - transport_global->stream_lookahead; |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1524 | } else { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1525 | max_recv_bytes = (uint32_t)max_size_hint; |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /* account for bytes already received but unknown to higher layers */ |
| 1529 | if (max_recv_bytes >= have_already) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1530 | max_recv_bytes -= (uint32_t)have_already; |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1531 | } else { |
| 1532 | max_recv_bytes = 0; |
| 1533 | } |
| 1534 | |
| 1535 | /* add some small lookahead to keep pipelines flowing */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1536 | GPR_ASSERT(max_recv_bytes <= UINT32_MAX - transport_global->stream_lookahead); |
Craig Tiller | a8d6809 | 2015-11-17 08:02:29 -0800 | [diff] [blame] | 1537 | max_recv_bytes += transport_global->stream_lookahead; |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1538 | if (stream_global->max_recv_bytes < max_recv_bytes) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1539 | uint32_t add_max_recv_bytes = |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1540 | max_recv_bytes - stream_global->max_recv_bytes; |
| 1541 | GRPC_CHTTP2_FLOW_CREDIT_STREAM("op", transport_global, stream_global, |
| 1542 | max_recv_bytes, add_max_recv_bytes); |
| 1543 | GRPC_CHTTP2_FLOW_CREDIT_STREAM("op", transport_global, stream_global, |
| 1544 | unannounced_incoming_window_for_parse, |
| 1545 | add_max_recv_bytes); |
| 1546 | GRPC_CHTTP2_FLOW_CREDIT_STREAM("op", transport_global, stream_global, |
| 1547 | unannounced_incoming_window_for_writing, |
| 1548 | add_max_recv_bytes); |
| 1549 | grpc_chttp2_list_add_unannounced_incoming_window_available(transport_global, |
| 1550 | stream_global); |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1551 | grpc_chttp2_become_writable(transport_global, stream_global); |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1555 | static int incoming_byte_stream_next(grpc_exec_ctx *exec_ctx, |
| 1556 | grpc_byte_stream *byte_stream, |
| 1557 | gpr_slice *slice, size_t max_size_hint, |
| 1558 | grpc_closure *on_complete) { |
| 1559 | grpc_chttp2_incoming_byte_stream *bs = |
| 1560 | (grpc_chttp2_incoming_byte_stream *)byte_stream; |
| 1561 | grpc_chttp2_transport_global *transport_global = &bs->transport->global; |
| 1562 | grpc_chttp2_stream_global *stream_global = &bs->stream->global; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1563 | |
| 1564 | lock(bs->transport); |
| 1565 | if (bs->is_tail) { |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1566 | incoming_byte_stream_update_flow_control(transport_global, stream_global, |
| 1567 | max_size_hint, bs->slices.length); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1568 | } |
| 1569 | if (bs->slices.count > 0) { |
| 1570 | *slice = gpr_slice_buffer_take_first(&bs->slices); |
| 1571 | unlock(exec_ctx, bs->transport); |
| 1572 | return 1; |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1573 | } else if (bs->failed) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 1574 | grpc_exec_ctx_enqueue(exec_ctx, on_complete, false, NULL); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1575 | unlock(exec_ctx, bs->transport); |
| 1576 | return 0; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1577 | } else { |
| 1578 | bs->on_next = on_complete; |
| 1579 | bs->next = slice; |
| 1580 | unlock(exec_ctx, bs->transport); |
| 1581 | return 0; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | static void incoming_byte_stream_unref(grpc_chttp2_incoming_byte_stream *bs) { |
| 1586 | if (gpr_unref(&bs->refs)) { |
| 1587 | gpr_slice_buffer_destroy(&bs->slices); |
| 1588 | gpr_free(bs); |
| 1589 | } |
| 1590 | } |
| 1591 | |
Craig Tiller | a3f298f | 2015-12-16 19:42:09 -0800 | [diff] [blame] | 1592 | static void incoming_byte_stream_destroy(grpc_exec_ctx *exec_ctx, |
| 1593 | grpc_byte_stream *byte_stream) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1594 | incoming_byte_stream_unref((grpc_chttp2_incoming_byte_stream *)byte_stream); |
| 1595 | } |
| 1596 | |
| 1597 | void grpc_chttp2_incoming_byte_stream_push(grpc_exec_ctx *exec_ctx, |
| 1598 | grpc_chttp2_incoming_byte_stream *bs, |
| 1599 | gpr_slice slice) { |
| 1600 | gpr_mu_lock(&bs->transport->mu); |
| 1601 | if (bs->on_next != NULL) { |
| 1602 | *bs->next = slice; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 1603 | grpc_exec_ctx_enqueue(exec_ctx, bs->on_next, true, NULL); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1604 | bs->on_next = NULL; |
| 1605 | } else { |
| 1606 | gpr_slice_buffer_add(&bs->slices, slice); |
| 1607 | } |
| 1608 | gpr_mu_unlock(&bs->transport->mu); |
| 1609 | } |
| 1610 | |
| 1611 | void grpc_chttp2_incoming_byte_stream_finished( |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1612 | grpc_exec_ctx *exec_ctx, grpc_chttp2_incoming_byte_stream *bs, int success, |
| 1613 | int from_parsing_thread) { |
| 1614 | if (!success) { |
| 1615 | if (from_parsing_thread) { |
| 1616 | gpr_mu_lock(&bs->transport->mu); |
| 1617 | } |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 1618 | grpc_exec_ctx_enqueue(exec_ctx, bs->on_next, false, NULL); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1619 | bs->on_next = NULL; |
| 1620 | bs->failed = 1; |
| 1621 | if (from_parsing_thread) { |
| 1622 | gpr_mu_unlock(&bs->transport->mu); |
| 1623 | } |
| 1624 | } else { |
| 1625 | #ifndef NDEBUG |
| 1626 | if (from_parsing_thread) { |
| 1627 | gpr_mu_lock(&bs->transport->mu); |
| 1628 | } |
| 1629 | GPR_ASSERT(bs->on_next == NULL); |
| 1630 | if (from_parsing_thread) { |
| 1631 | gpr_mu_unlock(&bs->transport->mu); |
| 1632 | } |
| 1633 | #endif |
| 1634 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1635 | incoming_byte_stream_unref(bs); |
| 1636 | } |
| 1637 | |
| 1638 | grpc_chttp2_incoming_byte_stream *grpc_chttp2_incoming_byte_stream_create( |
Craig Tiller | 20df14e | 2015-11-06 09:10:49 -0800 | [diff] [blame] | 1639 | grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_parsing *transport_parsing, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1640 | grpc_chttp2_stream_parsing *stream_parsing, uint32_t frame_size, |
| 1641 | uint32_t flags, grpc_chttp2_incoming_frame_queue *add_to_queue) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1642 | grpc_chttp2_incoming_byte_stream *incoming_byte_stream = |
| 1643 | gpr_malloc(sizeof(*incoming_byte_stream)); |
| 1644 | incoming_byte_stream->base.length = frame_size; |
| 1645 | incoming_byte_stream->base.flags = flags; |
| 1646 | incoming_byte_stream->base.next = incoming_byte_stream_next; |
| 1647 | incoming_byte_stream->base.destroy = incoming_byte_stream_destroy; |
| 1648 | gpr_ref_init(&incoming_byte_stream->refs, 2); |
| 1649 | incoming_byte_stream->next_message = NULL; |
| 1650 | incoming_byte_stream->transport = TRANSPORT_FROM_PARSING(transport_parsing); |
| 1651 | incoming_byte_stream->stream = STREAM_FROM_PARSING(stream_parsing); |
| 1652 | gpr_slice_buffer_init(&incoming_byte_stream->slices); |
| 1653 | incoming_byte_stream->on_next = NULL; |
| 1654 | incoming_byte_stream->is_tail = 1; |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1655 | incoming_byte_stream->failed = 0; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1656 | if (add_to_queue->head == NULL) { |
| 1657 | add_to_queue->head = incoming_byte_stream; |
| 1658 | } else { |
| 1659 | add_to_queue->tail->is_tail = 0; |
| 1660 | add_to_queue->tail->next_message = incoming_byte_stream; |
| 1661 | } |
| 1662 | add_to_queue->tail = incoming_byte_stream; |
| 1663 | return incoming_byte_stream; |
| 1664 | } |
| 1665 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1666 | /******************************************************************************* |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 1667 | * TRACING |
| 1668 | */ |
| 1669 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1670 | static char *format_flowctl_context_var(const char *context, const char *var, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1671 | int64_t val, uint32_t id, |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1672 | char **scope) { |
| 1673 | char *underscore_pos; |
| 1674 | char *result; |
| 1675 | if (context == NULL) { |
| 1676 | *scope = NULL; |
| 1677 | gpr_asprintf(&result, "%s(%lld)", var, val); |
| 1678 | return result; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1679 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1680 | underscore_pos = strchr(context, '_'); |
| 1681 | *scope = gpr_strdup(context); |
| 1682 | (*scope)[underscore_pos - context] = 0; |
| 1683 | if (id != 0) { |
| 1684 | char *tmp = *scope; |
| 1685 | gpr_asprintf(scope, "%s[%d]", tmp, id); |
| 1686 | gpr_free(tmp); |
| 1687 | } |
| 1688 | gpr_asprintf(&result, "%s.%s(%lld)", underscore_pos + 1, var, val); |
| 1689 | return result; |
| 1690 | } |
| 1691 | |
| 1692 | static int samestr(char *a, char *b) { |
| 1693 | if (a == NULL) { |
| 1694 | return b == NULL; |
| 1695 | } |
| 1696 | if (b == NULL) { |
| 1697 | return 0; |
| 1698 | } |
| 1699 | return 0 == strcmp(a, b); |
| 1700 | } |
| 1701 | |
| 1702 | void grpc_chttp2_flowctl_trace(const char *file, int line, const char *phase, |
| 1703 | grpc_chttp2_flowctl_op op, const char *context1, |
| 1704 | const char *var1, const char *context2, |
| 1705 | const char *var2, int is_client, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1706 | uint32_t stream_id, int64_t val1, int64_t val2) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1707 | char *scope1; |
| 1708 | char *scope2; |
| 1709 | char *label1 = |
| 1710 | format_flowctl_context_var(context1, var1, val1, stream_id, &scope1); |
| 1711 | char *label2 = |
| 1712 | format_flowctl_context_var(context2, var2, val2, stream_id, &scope2); |
| 1713 | char *clisvr = is_client ? "client" : "server"; |
| 1714 | char *prefix; |
| 1715 | |
| 1716 | gpr_asprintf(&prefix, "FLOW % 8s: %s % 11s ", phase, clisvr, scope1); |
| 1717 | |
| 1718 | switch (op) { |
| 1719 | case GRPC_CHTTP2_FLOWCTL_MOVE: |
| 1720 | GPR_ASSERT(samestr(scope1, scope2)); |
| 1721 | if (val2 != 0) { |
| 1722 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 1723 | "%sMOVE % 40s <- % 40s giving %d", prefix, label1, label2, |
| 1724 | val1 + val2); |
| 1725 | } |
| 1726 | break; |
| 1727 | case GRPC_CHTTP2_FLOWCTL_CREDIT: |
| 1728 | GPR_ASSERT(val2 >= 0); |
| 1729 | if (val2 != 0) { |
| 1730 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 1731 | "%sCREDIT % 40s by % 40s giving %d", prefix, label1, label2, |
| 1732 | val1 + val2); |
| 1733 | } |
| 1734 | break; |
| 1735 | case GRPC_CHTTP2_FLOWCTL_DEBIT: |
| 1736 | GPR_ASSERT(val2 >= 0); |
| 1737 | if (val2 != 0) { |
| 1738 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 1739 | "%sDEBIT % 40s by % 40s giving %d", prefix, label1, label2, |
| 1740 | val1 - val2); |
| 1741 | } |
| 1742 | break; |
| 1743 | } |
| 1744 | |
| 1745 | gpr_free(scope1); |
| 1746 | gpr_free(scope2); |
| 1747 | gpr_free(label1); |
| 1748 | gpr_free(label2); |
| 1749 | gpr_free(prefix); |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 1752 | /******************************************************************************* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1753 | * INTEGRATION GLUE |
| 1754 | */ |
| 1755 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1756 | static char *chttp2_get_peer(grpc_exec_ctx *exec_ctx, grpc_transport *t) { |
| 1757 | return gpr_strdup(((grpc_chttp2_transport *)t)->peer_string); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1760 | static const grpc_transport_vtable vtable = {sizeof(grpc_chttp2_stream), |
| 1761 | "chttp2", |
| 1762 | init_stream, |
| 1763 | set_pollset, |
| 1764 | perform_stream_op, |
| 1765 | perform_transport_op, |
| 1766 | destroy_stream, |
| 1767 | destroy_transport, |
| 1768 | chttp2_get_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1769 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1770 | grpc_transport *grpc_create_chttp2_transport( |
| 1771 | grpc_exec_ctx *exec_ctx, const grpc_channel_args *channel_args, |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1772 | grpc_endpoint *ep, int is_client) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1773 | grpc_chttp2_transport *t = gpr_malloc(sizeof(grpc_chttp2_transport)); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1774 | init_transport(exec_ctx, t, channel_args, ep, is_client != 0); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 1775 | return &t->base; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1776 | } |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 1777 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1778 | void grpc_chttp2_transport_start_reading(grpc_exec_ctx *exec_ctx, |
| 1779 | grpc_transport *transport, |
| 1780 | gpr_slice *slices, size_t nslices) { |
| 1781 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)transport; |
| 1782 | REF_TRANSPORT(t, "recv_data"); /* matches unref inside recv_data */ |
| 1783 | gpr_slice_buffer_addn(&t->read_buffer, slices, nslices); |
| 1784 | recv_data(exec_ctx, t, 1); |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 1785 | } |