Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 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" |
| 52 | #include "src/core/transport/transport_impl.h" |
| 53 | |
ctiller | 493fbcc | 2014-12-07 15:09:10 -0800 | [diff] [blame] | 54 | #define DEFAULT_WINDOW 65535 |
| 55 | #define DEFAULT_CONNECTION_WINDOW_TARGET (1024 * 1024) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | #define MAX_WINDOW 0x7fffffffu |
| 57 | |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 58 | #define MAX_CLIENT_STREAM_ID 0x7fffffffu |
| 59 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 60 | int grpc_http_trace = 0; |
Craig Tiller | 6a8c038 | 2015-04-28 10:01:22 -0700 | [diff] [blame] | 61 | int grpc_flowctl_trace = 0; |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 62 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 63 | #define TRANSPORT_FROM_WRITING(tw) \ |
| 64 | ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ |
| 65 | writing))) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | |
Craig Tiller | cfb5db9 | 2015-06-15 17:14:41 -0700 | [diff] [blame] | 67 | #define TRANSPORT_FROM_PARSING(tw) \ |
| 68 | ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \ |
| 69 | parsing))) |
Craig Tiller | d50e565 | 2015-02-24 16:46:22 -0800 | [diff] [blame] | 70 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 71 | #define TRANSPORT_FROM_GLOBAL(tg) \ |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 72 | ((grpc_chttp2_transport *)((char *)(tg)-offsetof(grpc_chttp2_transport, \ |
| 73 | global))) |
Craig Tiller | 6a8c038 | 2015-04-28 10:01:22 -0700 | [diff] [blame] | 74 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 75 | #define STREAM_FROM_GLOBAL(sg) \ |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 76 | ((grpc_chttp2_stream *)((char *)(sg)-offsetof(grpc_chttp2_stream, global))) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | |
| 78 | static const grpc_transport_vtable vtable; |
| 79 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 80 | static void lock(grpc_chttp2_transport *t); |
| 81 | static void unlock(grpc_chttp2_transport *t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 83 | static void unlock_check_read_write_state(grpc_chttp2_transport *t); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 84 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 85 | /* forward declarations of various callbacks that we'll build closures around */ |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 86 | static void writing_action(void *t, int iomgr_success_ignored); |
Craig Tiller | 8b2f1d7 | 2015-06-18 13:38:38 -0700 | [diff] [blame] | 87 | static void reading_action(void *t, int iomgr_success_ignored); |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 88 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 89 | /** Set a transport level setting, and push it to our peer */ |
| 90 | static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | gpr_uint32 value); |
| 92 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 93 | /** Endpoint callback to process incoming data */ |
Nicolas Noble | 5ea99bb | 2015-02-04 14:13:09 -0800 | [diff] [blame] | 94 | static void recv_data(void *tp, gpr_slice *slices, size_t nslices, |
| 95 | grpc_endpoint_cb_status error); |
| 96 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 97 | /** Start disconnection chain */ |
| 98 | static void drop_connection(grpc_chttp2_transport *t); |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 99 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 100 | /** Perform a transport_op */ |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 101 | static void perform_stream_op_locked( |
| 102 | grpc_chttp2_transport_global *transport_global, |
| 103 | grpc_chttp2_stream_global *stream_global, grpc_transport_stream_op *op); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 104 | |
| 105 | /** Cancel a stream: coming from the transport API */ |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 106 | static void cancel_from_api(grpc_chttp2_transport_global *transport_global, |
| 107 | grpc_chttp2_stream_global *stream_global, |
| 108 | grpc_status_code status); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 109 | |
| 110 | /** Add endpoint from this transport to pollset */ |
| 111 | static void add_to_pollset_locked(grpc_chttp2_transport *t, |
| 112 | grpc_pollset *pollset); |
| 113 | |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 114 | /** Start new streams that have been created if we can */ |
| 115 | static void maybe_start_some_streams( |
| 116 | grpc_chttp2_transport_global *transport_global); |
Craig Tiller | 6a8c038 | 2015-04-28 10:01:22 -0700 | [diff] [blame] | 117 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 118 | static void connectivity_state_set( |
| 119 | grpc_chttp2_transport_global *transport_global, |
| 120 | grpc_connectivity_state state); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 121 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | /* |
| 123 | * CONSTRUCTION/DESTRUCTION/REFCOUNTING |
| 124 | */ |
| 125 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 126 | static void destruct_transport(grpc_chttp2_transport *t) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | size_t i; |
| 128 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | gpr_mu_lock(&t->mu); |
| 130 | |
| 131 | GPR_ASSERT(t->ep == NULL); |
| 132 | |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 133 | gpr_slice_buffer_destroy(&t->global.qbuf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 134 | |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 135 | gpr_slice_buffer_destroy(&t->writing.outbuf); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 136 | grpc_chttp2_hpack_compressor_destroy(&t->writing.hpack_compressor); |
| 137 | |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 138 | gpr_slice_buffer_destroy(&t->parsing.qbuf); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 139 | grpc_chttp2_hpack_parser_destroy(&t->parsing.hpack_parser); |
| 140 | grpc_chttp2_goaway_parser_destroy(&t->parsing.goaway_parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 142 | GRPC_MDSTR_UNREF(t->parsing.str_grpc_timeout); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | |
| 144 | for (i = 0; i < STREAM_LIST_COUNT; i++) { |
| 145 | GPR_ASSERT(t->lists[i].head == NULL); |
| 146 | GPR_ASSERT(t->lists[i].tail == NULL); |
| 147 | } |
| 148 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 149 | GPR_ASSERT(grpc_chttp2_stream_map_size(&t->parsing_stream_map) == 0); |
| 150 | GPR_ASSERT(grpc_chttp2_stream_map_size(&t->new_stream_map) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 152 | grpc_chttp2_stream_map_destroy(&t->parsing_stream_map); |
| 153 | grpc_chttp2_stream_map_destroy(&t->new_stream_map); |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 154 | grpc_connectivity_state_destroy(&t->channel_callback.state_tracker); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | |
| 156 | gpr_mu_unlock(&t->mu); |
| 157 | gpr_mu_destroy(&t->mu); |
| 158 | |
| 159 | /* callback remaining pings: they're not allowed to call into the transpot, |
| 160 | and maybe they hold resources that need to be freed */ |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 161 | while (t->global.pings.next != &t->global.pings) { |
| 162 | grpc_chttp2_outstanding_ping *ping = t->global.pings.next; |
| 163 | grpc_iomgr_add_delayed_callback(ping->on_recv, 0); |
| 164 | ping->next->prev = ping->prev; |
| 165 | ping->prev->next = ping->next; |
| 166 | gpr_free(ping); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 167 | } |
Craig Tiller | 8ed35ea | 2015-01-30 11:27:43 -0800 | [diff] [blame] | 168 | |
Craig Tiller | 9be83ee | 2015-02-18 14:16:15 -0800 | [diff] [blame] | 169 | grpc_mdctx_unref(t->metadata_context); |
| 170 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 171 | gpr_free(t->peer_string); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 172 | gpr_free(t); |
| 173 | } |
| 174 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 175 | #ifdef REFCOUNTING_DEBUG |
| 176 | #define REF_TRANSPORT(t, r) ref_transport(t, r, __FILE__, __LINE__) |
| 177 | #define UNREF_TRANSPORT(t, r) unref_transport(t, r, __FILE__, __LINE__) |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 178 | static void unref_transport(grpc_chttp2_transport *t, const char *reason, |
| 179 | const char *file, int line) { |
| 180 | gpr_log(GPR_DEBUG, "chttp2:unref:%p %d->%d %s [%s:%d]", t, t->refs.count, |
| 181 | t->refs.count - 1, reason, file, line); |
Craig Tiller | 9be83ee | 2015-02-18 14:16:15 -0800 | [diff] [blame] | 182 | if (!gpr_unref(&t->refs)) return; |
| 183 | destruct_transport(t); |
| 184 | } |
| 185 | |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 186 | static void ref_transport(grpc_chttp2_transport *t, const char *reason, |
| 187 | const char *file, int line) { |
| 188 | gpr_log(GPR_DEBUG, "chttp2: ref:%p %d->%d %s [%s:%d]", t, t->refs.count, |
| 189 | t->refs.count + 1, reason, file, line); |
| 190 | gpr_ref(&t->refs); |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 191 | } |
| 192 | #else |
| 193 | #define REF_TRANSPORT(t, r) ref_transport(t) |
| 194 | #define UNREF_TRANSPORT(t, r) unref_transport(t) |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 195 | static void unref_transport(grpc_chttp2_transport *t) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | if (!gpr_unref(&t->refs)) return; |
| 197 | destruct_transport(t); |
| 198 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 200 | static void ref_transport(grpc_chttp2_transport *t) { gpr_ref(&t->refs); } |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 201 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 202 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 203 | static void init_transport(grpc_chttp2_transport *t, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 204 | const grpc_channel_args *channel_args, |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 205 | grpc_endpoint *ep, grpc_mdctx *mdctx, |
| 206 | int is_client) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 207 | size_t i; |
| 208 | int j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 210 | GPR_ASSERT(strlen(GRPC_CHTTP2_CLIENT_CONNECT_STRING) == |
| 211 | GRPC_CHTTP2_CLIENT_CONNECT_STRLEN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 213 | memset(t, 0, sizeof(*t)); |
| 214 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 215 | t->base.vtable = &vtable; |
| 216 | t->ep = ep; |
| 217 | /* one ref is for destroy, the other for when ep becomes NULL */ |
| 218 | gpr_ref_init(&t->refs, 2); |
| 219 | gpr_mu_init(&t->mu); |
Craig Tiller | 9be83ee | 2015-02-18 14:16:15 -0800 | [diff] [blame] | 220 | grpc_mdctx_ref(mdctx); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 221 | t->peer_string = grpc_endpoint_get_peer(ep); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | t->metadata_context = mdctx; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 223 | t->endpoint_reading = 1; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 224 | t->global.next_stream_id = is_client ? 1 : 2; |
| 225 | t->global.is_client = is_client; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 226 | t->global.outgoing_window = DEFAULT_WINDOW; |
| 227 | t->global.incoming_window = DEFAULT_WINDOW; |
| 228 | t->global.connection_window_target = DEFAULT_CONNECTION_WINDOW_TARGET; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 229 | t->global.ping_counter = 1; |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 230 | t->global.pings.next = t->global.pings.prev = &t->global.pings; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 231 | t->parsing.is_client = is_client; |
| 232 | t->parsing.str_grpc_timeout = |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 233 | grpc_mdstr_from_string(t->metadata_context, "grpc-timeout"); |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 234 | t->parsing.deframe_state = |
| 235 | is_client ? GRPC_DTS_FH_0 : GRPC_DTS_CLIENT_PREFIX_0; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 236 | t->writing.is_client = is_client; |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 237 | grpc_connectivity_state_init(&t->channel_callback.state_tracker, |
| 238 | GRPC_CHANNEL_READY); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 239 | |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 240 | gpr_slice_buffer_init(&t->global.qbuf); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 241 | |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 242 | gpr_slice_buffer_init(&t->writing.outbuf); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 243 | grpc_chttp2_hpack_compressor_init(&t->writing.hpack_compressor, mdctx); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 244 | grpc_iomgr_closure_init(&t->writing_action, writing_action, t); |
Craig Tiller | 8b2f1d7 | 2015-06-18 13:38:38 -0700 | [diff] [blame] | 245 | grpc_iomgr_closure_init(&t->reading_action, reading_action, t); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 246 | |
Craig Tiller | 99f8055 | 2015-06-11 16:26:03 -0700 | [diff] [blame] | 247 | gpr_slice_buffer_init(&t->parsing.qbuf); |
Craig Tiller | 42cdf94 | 2015-06-12 07:35:44 -0700 | [diff] [blame] | 248 | grpc_chttp2_goaway_parser_init(&t->parsing.goaway_parser); |
| 249 | grpc_chttp2_hpack_parser_init(&t->parsing.hpack_parser, t->metadata_context); |
| 250 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 251 | if (is_client) { |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 252 | gpr_slice_buffer_add( |
| 253 | &t->global.qbuf, |
| 254 | gpr_slice_from_copied_string(GRPC_CHTTP2_CLIENT_CONNECT_STRING)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 255 | } |
| 256 | /* 8 is a random stab in the dark as to a good initial size: it's small enough |
| 257 | that it shouldn't waste memory for infrequently used connections, yet |
| 258 | large enough that the exponential growth should happen nicely when it's |
| 259 | needed. |
| 260 | TODO(ctiller): tune this */ |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 261 | grpc_chttp2_stream_map_init(&t->parsing_stream_map, 8); |
| 262 | grpc_chttp2_stream_map_init(&t->new_stream_map, 8); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 263 | |
| 264 | /* copy in initial settings to all setting sets */ |
Craig Tiller | cdf52bc | 2015-06-16 13:00:27 -0700 | [diff] [blame] | 265 | for (i = 0; i < GRPC_CHTTP2_NUM_SETTINGS; i++) { |
| 266 | t->parsing.settings[i] = grpc_chttp2_settings_parameters[i].default_value; |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 267 | for (j = 0; j < GRPC_NUM_SETTING_SETS; j++) { |
Craig Tiller | cdf52bc | 2015-06-16 13:00:27 -0700 | [diff] [blame] | 268 | t->global.settings[j][i] = |
| 269 | grpc_chttp2_settings_parameters[i].default_value; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 272 | t->global.dirtied_local_settings = 1; |
ctiller | 493fbcc | 2014-12-07 15:09:10 -0800 | [diff] [blame] | 273 | /* Hack: it's common for implementations to assume 65536 bytes initial send |
| 274 | window -- this should by rights be 0 */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 275 | t->global.force_send_settings = 1 << GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; |
| 276 | t->global.sent_local_settings = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 277 | |
| 278 | /* configure http2 the way we like it */ |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 279 | if (is_client) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 280 | push_setting(t, GRPC_CHTTP2_SETTINGS_ENABLE_PUSH, 0); |
| 281 | push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 0); |
| 282 | } |
ctiller | 493fbcc | 2014-12-07 15:09:10 -0800 | [diff] [blame] | 283 | push_setting(t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, DEFAULT_WINDOW); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | |
| 285 | if (channel_args) { |
| 286 | for (i = 0; i < channel_args->num_args; i++) { |
| 287 | if (0 == |
| 288 | strcmp(channel_args->args[i].key, GRPC_ARG_MAX_CONCURRENT_STREAMS)) { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 289 | if (is_client) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | gpr_log(GPR_ERROR, "%s: is ignored on the client", |
| 291 | GRPC_ARG_MAX_CONCURRENT_STREAMS); |
| 292 | } else if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
| 293 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 294 | GRPC_ARG_MAX_CONCURRENT_STREAMS); |
| 295 | } else { |
| 296 | push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, |
| 297 | channel_args->args[i].value.integer); |
| 298 | } |
Craig Tiller | 8802558 | 2015-05-04 09:41:10 -0700 | [diff] [blame] | 299 | } else if (0 == strcmp(channel_args->args[i].key, |
| 300 | GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER)) { |
| 301 | if (channel_args->args[i].type != GRPC_ARG_INTEGER) { |
| 302 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 303 | GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 304 | } else if ((t->global.next_stream_id & 1) != |
Craig Tiller | 8802558 | 2015-05-04 09:41:10 -0700 | [diff] [blame] | 305 | (channel_args->args[i].value.integer & 1)) { |
| 306 | gpr_log(GPR_ERROR, "%s: low bit must be %d on %s", |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 307 | GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, |
| 308 | t->global.next_stream_id & 1, |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 309 | is_client ? "client" : "server"); |
Craig Tiller | 8802558 | 2015-05-04 09:41:10 -0700 | [diff] [blame] | 310 | } else { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 311 | t->global.next_stream_id = channel_args->args[i].value.integer; |
Craig Tiller | 8802558 | 2015-05-04 09:41:10 -0700 | [diff] [blame] | 312 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void destroy_transport(grpc_transport *gt) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 319 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 320 | |
Craig Tiller | 748fe3f | 2015-03-02 07:48:50 -0800 | [diff] [blame] | 321 | lock(t); |
Craig Tiller | 1fe7b9d | 2015-02-17 11:57:02 -0800 | [diff] [blame] | 322 | t->destroying = 1; |
Craig Tiller | 748fe3f | 2015-03-02 07:48:50 -0800 | [diff] [blame] | 323 | drop_connection(t); |
| 324 | unlock(t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 325 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 326 | UNREF_TRANSPORT(t, "destroy"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 329 | static void close_transport_locked(grpc_chttp2_transport *t) { |
Craig Tiller | ec3aa89 | 2015-05-29 17:22:17 -0700 | [diff] [blame] | 330 | if (!t->closed) { |
| 331 | t->closed = 1; |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 332 | connectivity_state_set(&t->global, GRPC_CHANNEL_FATAL_FAILURE); |
Craig Tiller | ec3aa89 | 2015-05-29 17:22:17 -0700 | [diff] [blame] | 333 | if (t->ep) { |
| 334 | grpc_endpoint_shutdown(t->ep); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 339 | static int init_stream(grpc_transport *gt, grpc_stream *gs, |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 340 | const void *server_data, |
| 341 | grpc_transport_stream_op *initial_op) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 342 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 343 | grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 345 | memset(s, 0, sizeof(*s)); |
| 346 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 347 | grpc_chttp2_incoming_metadata_buffer_init(&s->parsing.incoming_metadata); |
| 348 | grpc_chttp2_incoming_metadata_buffer_init(&s->global.incoming_metadata); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 349 | grpc_sopb_init(&s->writing.sopb); |
Craig Tiller | f73fcd1 | 2015-06-16 16:25:26 -0700 | [diff] [blame] | 350 | grpc_sopb_init(&s->global.incoming_sopb); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 351 | grpc_chttp2_data_parser_init(&s->parsing.data_parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 352 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 353 | REF_TRANSPORT(t, "stream"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 354 | |
Craig Tiller | e061762 | 2015-06-11 09:37:17 -0700 | [diff] [blame] | 355 | lock(t); |
Craig Tiller | 6459db4 | 2015-06-15 17:11:45 -0700 | [diff] [blame] | 356 | grpc_chttp2_register_stream(t, s); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 357 | if (server_data) { |
| 358 | GPR_ASSERT(t->parsing_active); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 359 | s->global.id = (gpr_uint32)(gpr_uintptr)server_data; |
| 360 | s->global.outgoing_window = |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 361 | t->global.settings[GRPC_PEER_SETTINGS] |
| 362 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | 27166a6 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 363 | s->global.max_recv_bytes = |
| 364 | s->parsing.incoming_window = |
| 365 | s->global.incoming_window = |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 366 | t->global.settings[GRPC_SENT_SETTINGS] |
| 367 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 368 | *t->accepting_stream = s; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 369 | grpc_chttp2_stream_map_add(&t->parsing_stream_map, s->global.id, s); |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 370 | s->global.in_stream_map = 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 373 | if (initial_op) perform_stream_op_locked(&t->global, &s->global, initial_op); |
Craig Tiller | e061762 | 2015-06-11 09:37:17 -0700 | [diff] [blame] | 374 | unlock(t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static void destroy_stream(grpc_transport *gt, grpc_stream *gs) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 380 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 381 | grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; |
Craig Tiller | c3fdaf9 | 2015-06-18 10:16:52 -0700 | [diff] [blame] | 382 | int i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 383 | |
| 384 | gpr_mu_lock(&t->mu); |
| 385 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 386 | GPR_ASSERT(s->global.published_state == GRPC_STREAM_CLOSED || |
| 387 | s->global.id == 0); |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 388 | GPR_ASSERT(!s->global.in_stream_map); |
Craig Tiller | 9188d7a | 2015-07-05 12:44:37 -0700 | [diff] [blame] | 389 | if (grpc_chttp2_unregister_stream(t, s) && t->global.sent_goaway) { |
| 390 | close_transport_locked(t); |
| 391 | } |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 392 | if (!t->parsing_active && s->global.id) { |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 393 | GPR_ASSERT(grpc_chttp2_stream_map_find(&t->parsing_stream_map, |
| 394 | s->global.id) == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Craig Tiller | c3fdaf9 | 2015-06-18 10:16:52 -0700 | [diff] [blame] | 397 | grpc_chttp2_list_remove_incoming_window_updated(&t->global, &s->global); |
Craig Tiller | e3086f8 | 2015-07-23 16:25:00 -0700 | [diff] [blame] | 398 | grpc_chttp2_list_remove_writable_stream(&t->global, &s->global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 399 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 400 | gpr_mu_unlock(&t->mu); |
| 401 | |
Craig Tiller | c3fdaf9 | 2015-06-18 10:16:52 -0700 | [diff] [blame] | 402 | for (i = 0; i < STREAM_LIST_COUNT; i++) { |
Craig Tiller | 05cc0c4 | 2015-07-23 16:01:27 -0700 | [diff] [blame] | 403 | if (s->included[i]) { |
| 404 | gpr_log(GPR_ERROR, "%s stream %d still included in list %d", |
| 405 | t->global.is_client ? "client" : "server", s->global.id, i); |
| 406 | abort(); |
| 407 | } |
Craig Tiller | 9c71b6f | 2015-04-24 16:02:00 -0700 | [diff] [blame] | 408 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 409 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 410 | GPR_ASSERT(s->global.outgoing_sopb == NULL); |
Craig Tiller | 6905b7c | 2015-06-16 15:33:33 -0700 | [diff] [blame] | 411 | GPR_ASSERT(s->global.publish_sopb == NULL); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 412 | grpc_sopb_destroy(&s->writing.sopb); |
Craig Tiller | 0b442a8 | 2015-06-30 17:04:32 -0700 | [diff] [blame] | 413 | grpc_sopb_destroy(&s->global.incoming_sopb); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 414 | grpc_chttp2_data_parser_destroy(&s->parsing.data_parser); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 415 | grpc_chttp2_incoming_metadata_buffer_destroy(&s->parsing.incoming_metadata); |
| 416 | grpc_chttp2_incoming_metadata_buffer_destroy(&s->global.incoming_metadata); |
Craig Tiller | 47073d5 | 2015-06-30 10:19:22 -0700 | [diff] [blame] | 417 | grpc_chttp2_incoming_metadata_live_op_buffer_end( |
| 418 | &s->global.outstanding_metadata); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 419 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 420 | UNREF_TRANSPORT(t, "stream"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Craig Tiller | cfb5db9 | 2015-06-15 17:14:41 -0700 | [diff] [blame] | 423 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream( |
| 424 | grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id) { |
| 425 | grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 426 | grpc_chttp2_stream *s = |
| 427 | grpc_chttp2_stream_map_find(&t->parsing_stream_map, id); |
Craig Tiller | cf1e319 | 2015-06-16 14:28:22 -0700 | [diff] [blame] | 428 | return s ? &s->parsing : NULL; |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 431 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream( |
| 432 | grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id) { |
| 433 | grpc_chttp2_stream *accepting; |
| 434 | grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing); |
| 435 | GPR_ASSERT(t->accepting_stream == NULL); |
| 436 | t->accepting_stream = &accepting; |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 437 | t->channel_callback.accept_stream(t->channel_callback.accept_stream_user_data, |
| 438 | &t->base, (void *)(gpr_uintptr)id); |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 439 | t->accepting_stream = NULL; |
| 440 | return &accepting->parsing; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * LOCK MANAGEMENT |
| 445 | */ |
| 446 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 447 | /* We take a grpc_chttp2_transport-global lock in response to calls coming in |
| 448 | from above, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 449 | and in response to data being received from below. New data to be written |
| 450 | is always queued, as are callbacks to process data. During unlock() we |
| 451 | check our todo lists and initiate callbacks and flush writes. */ |
| 452 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 453 | static void lock(grpc_chttp2_transport *t) { gpr_mu_lock(&t->mu); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 454 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 455 | static void unlock(grpc_chttp2_transport *t) { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 456 | grpc_iomgr_closure *run_closures; |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 457 | |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 458 | unlock_check_read_write_state(t); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 459 | if (!t->writing_active && !t->closed && |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 460 | grpc_chttp2_unlocking_check_writes(&t->global, &t->writing)) { |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 461 | t->writing_active = 1; |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 462 | REF_TRANSPORT(t, "writing"); |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 463 | grpc_chttp2_schedule_closure(&t->global, &t->writing_action, 1); |
Craig Tiller | cb818ba | 2015-01-29 17:08:01 -0800 | [diff] [blame] | 464 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 465 | |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 466 | run_closures = t->global.pending_closures_head; |
| 467 | t->global.pending_closures_head = NULL; |
| 468 | t->global.pending_closures_tail = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 469 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 470 | gpr_mu_unlock(&t->mu); |
| 471 | |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 472 | while (run_closures) { |
| 473 | grpc_iomgr_closure *next = run_closures->next; |
| 474 | run_closures->cb(run_closures->cb_arg, run_closures->success); |
| 475 | run_closures = next; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 476 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* |
| 480 | * OUTPUT PROCESSING |
| 481 | */ |
| 482 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 483 | static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 484 | gpr_uint32 value) { |
| 485 | const grpc_chttp2_setting_parameters *sp = |
| 486 | &grpc_chttp2_settings_parameters[id]; |
| 487 | gpr_uint32 use_value = GPR_CLAMP(value, sp->min_value, sp->max_value); |
| 488 | if (use_value != value) { |
| 489 | gpr_log(GPR_INFO, "Requested parameter %s clamped from %d to %d", sp->name, |
| 490 | value, use_value); |
| 491 | } |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 492 | if (use_value != t->global.settings[GRPC_LOCAL_SETTINGS][id]) { |
| 493 | t->global.settings[GRPC_LOCAL_SETTINGS][id] = use_value; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 494 | t->global.dirtied_local_settings = 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 498 | void grpc_chttp2_terminate_writing( |
| 499 | grpc_chttp2_transport_writing *transport_writing, int success) { |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 500 | grpc_chttp2_transport *t = TRANSPORT_FROM_WRITING(transport_writing); |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 501 | |
| 502 | lock(t); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 503 | |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 504 | if (!success) { |
| 505 | drop_connection(t); |
| 506 | } |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 507 | |
| 508 | /* cleanup writing related jazz */ |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 509 | grpc_chttp2_cleanup_writing(&t->global, &t->writing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 510 | |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 511 | /* leave the writing flag up on shutdown to prevent further writes in unlock() |
| 512 | from starting */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 513 | t->writing_active = 0; |
Craig Tiller | f932fd5 | 2015-06-17 07:38:20 -0700 | [diff] [blame] | 514 | if (t->ep && !t->endpoint_reading) { |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 515 | grpc_endpoint_destroy(t->ep); |
| 516 | t->ep = NULL; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 517 | UNREF_TRANSPORT( |
| 518 | t, "disconnect"); /* safe because we'll still have the ref for write */ |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 519 | } |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 520 | |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 521 | unlock(t); |
| 522 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 523 | UNREF_TRANSPORT(t, "writing"); |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 526 | static void writing_action(void *gt, int iomgr_success_ignored) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 527 | grpc_chttp2_transport *t = gt; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 528 | grpc_chttp2_perform_writes(&t->writing, t->ep); |
ctiller | 00297df | 2015-01-12 11:23:09 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 531 | void grpc_chttp2_add_incoming_goaway( |
| 532 | grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error, |
| 533 | gpr_slice goaway_text) { |
Julien Boeuf | 0ac7cdd | 2015-06-30 14:53:05 +0200 | [diff] [blame] | 534 | char *msg = gpr_dump_slice(goaway_text, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 535 | gpr_log(GPR_DEBUG, "got goaway [%d]: %s", goaway_error, msg); |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 536 | gpr_free(msg); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 537 | gpr_slice_unref(goaway_text); |
| 538 | transport_global->seen_goaway = 1; |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 539 | connectivity_state_set(transport_global, GRPC_CHANNEL_FATAL_FAILURE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 542 | static void maybe_start_some_streams( |
| 543 | grpc_chttp2_transport_global *transport_global) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 544 | grpc_chttp2_stream_global *stream_global; |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 545 | /* start streams where we have free grpc_chttp2_stream ids and free |
| 546 | * concurrency */ |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 547 | while (transport_global->next_stream_id <= MAX_CLIENT_STREAM_ID && |
| 548 | transport_global->concurrent_stream_count < |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 549 | transport_global |
| 550 | ->settings[GRPC_PEER_SETTINGS] |
| 551 | [GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] && |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 552 | grpc_chttp2_list_pop_waiting_for_concurrency(transport_global, |
| 553 | &stream_global)) { |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 554 | GRPC_CHTTP2_IF_TRACING(gpr_log( |
| 555 | GPR_DEBUG, "HTTP:%s: Allocating new grpc_chttp2_stream %p to id %d", |
| 556 | transport_global->is_client ? "CLI" : "SVR", stream_global, |
| 557 | transport_global->next_stream_id)); |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 558 | |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 559 | GPR_ASSERT(stream_global->id == 0); |
| 560 | stream_global->id = transport_global->next_stream_id; |
| 561 | transport_global->next_stream_id += 2; |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 562 | |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 563 | if (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID) { |
Craig Tiller | 49924e0 | 2015-06-29 22:42:33 -0700 | [diff] [blame] | 564 | connectivity_state_set(transport_global, GRPC_CHANNEL_TRANSIENT_FAILURE); |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 567 | stream_global->outgoing_window = |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 568 | transport_global->settings[GRPC_PEER_SETTINGS] |
| 569 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 570 | stream_global->incoming_window = |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 571 | transport_global->settings[GRPC_SENT_SETTINGS] |
| 572 | [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]; |
Craig Tiller | 27166a6 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 573 | stream_global->max_recv_bytes = |
| 574 | GPR_MAX(stream_global->incoming_window, stream_global->max_recv_bytes); |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 575 | grpc_chttp2_stream_map_add( |
| 576 | &TRANSPORT_FROM_GLOBAL(transport_global)->new_stream_map, |
| 577 | stream_global->id, STREAM_FROM_GLOBAL(stream_global)); |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 578 | stream_global->in_stream_map = 1; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 579 | transport_global->concurrent_stream_count++; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 580 | grpc_chttp2_list_add_incoming_window_updated(transport_global, |
| 581 | stream_global); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 582 | grpc_chttp2_list_add_writable_stream(transport_global, stream_global); |
Craig Tiller | 27166a6 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 583 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 584 | } |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 585 | /* cancel out streams that will never be started */ |
Craig Tiller | 564872d | 2015-06-18 11:21:22 -0700 | [diff] [blame] | 586 | while (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID && |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 587 | grpc_chttp2_list_pop_waiting_for_concurrency(transport_global, |
| 588 | &stream_global)) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 589 | cancel_from_api(transport_global, stream_global, GRPC_STATUS_UNAVAILABLE); |
Craig Tiller | 7098c03 | 2015-05-04 10:18:28 -0700 | [diff] [blame] | 590 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 593 | static void perform_stream_op_locked( |
| 594 | grpc_chttp2_transport_global *transport_global, |
| 595 | grpc_chttp2_stream_global *stream_global, grpc_transport_stream_op *op) { |
Craig Tiller | 2ea37fd | 2015-04-24 13:03:49 -0700 | [diff] [blame] | 596 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 597 | cancel_from_api(transport_global, stream_global, op->cancel_with_status); |
Craig Tiller | 2ea37fd | 2015-04-24 13:03:49 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 600 | if (op->send_ops) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 601 | GPR_ASSERT(stream_global->outgoing_sopb == NULL); |
| 602 | stream_global->send_done_closure = op->on_done_send; |
| 603 | if (!stream_global->cancelled) { |
| 604 | stream_global->outgoing_sopb = op->send_ops; |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 605 | if (op->is_last_send && |
| 606 | stream_global->write_state == GRPC_WRITE_STATE_OPEN) { |
| 607 | stream_global->write_state = GRPC_WRITE_STATE_QUEUED_CLOSE; |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 608 | } |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 609 | if (stream_global->id == 0) { |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 610 | GRPC_CHTTP2_IF_TRACING(gpr_log( |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 611 | GPR_DEBUG, |
| 612 | "HTTP:%s: New grpc_chttp2_stream %p waiting for concurrency", |
| 613 | transport_global->is_client ? "CLI" : "SVR", stream_global)); |
| 614 | grpc_chttp2_list_add_waiting_for_concurrency(transport_global, |
| 615 | stream_global); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 616 | maybe_start_some_streams(transport_global); |
| 617 | } else if (stream_global->outgoing_window > 0) { |
| 618 | grpc_chttp2_list_add_writable_stream(transport_global, stream_global); |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 619 | } |
| 620 | } else { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 621 | grpc_sopb_reset(op->send_ops); |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 622 | grpc_chttp2_schedule_closure(transport_global, |
| 623 | stream_global->send_done_closure, 0); |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 624 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | if (op->recv_ops) { |
Craig Tiller | 6905b7c | 2015-06-16 15:33:33 -0700 | [diff] [blame] | 628 | GPR_ASSERT(stream_global->publish_sopb == NULL); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 629 | GPR_ASSERT(stream_global->published_state != GRPC_STREAM_CLOSED); |
| 630 | stream_global->recv_done_closure = op->on_done_recv; |
Craig Tiller | 6905b7c | 2015-06-16 15:33:33 -0700 | [diff] [blame] | 631 | stream_global->publish_sopb = op->recv_ops; |
| 632 | stream_global->publish_sopb->nops = 0; |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 633 | stream_global->publish_state = op->recv_state; |
Craig Tiller | 27166a6 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 634 | if (stream_global->max_recv_bytes < op->max_recv_bytes) { |
| 635 | GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("op", transport_global, stream_global, |
Craig Tiller | 27166a6 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 636 | max_recv_bytes, op->max_recv_bytes - stream_global->max_recv_bytes); |
Craig Tiller | 5c517b1 | 2015-07-15 13:28:10 -0700 | [diff] [blame] | 637 | GRPC_CHTTP2_FLOWCTL_TRACE_STREAM( |
| 638 | "op", transport_global, stream_global, unannounced_incoming_window, |
| 639 | op->max_recv_bytes - stream_global->max_recv_bytes); |
Craig Tiller | 27166a6 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 640 | stream_global->unannounced_incoming_window += op->max_recv_bytes - stream_global->max_recv_bytes; |
| 641 | stream_global->max_recv_bytes = op->max_recv_bytes; |
Craig Tiller | 4efb696 | 2015-06-03 09:32:41 -0700 | [diff] [blame] | 642 | } |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 643 | grpc_chttp2_incoming_metadata_live_op_buffer_end( |
| 644 | &stream_global->outstanding_metadata); |
Craig Tiller | 5c517b1 | 2015-07-15 13:28:10 -0700 | [diff] [blame] | 645 | if (stream_global->id != 0) { |
| 646 | grpc_chttp2_list_add_read_write_state_changed(transport_global, |
| 647 | stream_global); |
Craig Tiller | 994c262 | 2015-07-23 14:00:58 -0700 | [diff] [blame] | 648 | grpc_chttp2_list_add_writable_stream(transport_global, stream_global); |
Craig Tiller | 5c517b1 | 2015-07-15 13:28:10 -0700 | [diff] [blame] | 649 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (op->bind_pollset) { |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 653 | add_to_pollset_locked(TRANSPORT_FROM_GLOBAL(transport_global), |
| 654 | op->bind_pollset); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 655 | } |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 656 | |
| 657 | if (op->on_consumed) { |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 658 | grpc_chttp2_schedule_closure(transport_global, op->on_consumed, 1); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 659 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 660 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 661 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 662 | static void perform_stream_op(grpc_transport *gt, grpc_stream *gs, |
| 663 | grpc_transport_stream_op *op) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 664 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 665 | grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs; |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 666 | |
| 667 | lock(t); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 668 | perform_stream_op_locked(&t->global, &s->global, op); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 669 | unlock(t); |
| 670 | } |
| 671 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 672 | static void send_ping_locked(grpc_chttp2_transport *t, |
| 673 | grpc_iomgr_closure *on_recv) { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 674 | grpc_chttp2_outstanding_ping *p = gpr_malloc(sizeof(*p)); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 675 | p->next = &t->global.pings; |
| 676 | p->prev = p->next->prev; |
| 677 | p->prev->next = p->next->prev = p; |
| 678 | p->id[0] = (t->global.ping_counter >> 56) & 0xff; |
| 679 | p->id[1] = (t->global.ping_counter >> 48) & 0xff; |
| 680 | p->id[2] = (t->global.ping_counter >> 40) & 0xff; |
| 681 | p->id[3] = (t->global.ping_counter >> 32) & 0xff; |
| 682 | p->id[4] = (t->global.ping_counter >> 24) & 0xff; |
| 683 | p->id[5] = (t->global.ping_counter >> 16) & 0xff; |
| 684 | p->id[6] = (t->global.ping_counter >> 8) & 0xff; |
| 685 | p->id[7] = t->global.ping_counter & 0xff; |
| 686 | p->on_recv = on_recv; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 687 | 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] | 688 | } |
| 689 | |
| 690 | static void perform_transport_op(grpc_transport *gt, grpc_transport_op *op) { |
| 691 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt; |
| 692 | |
| 693 | lock(t); |
| 694 | |
| 695 | if (op->on_consumed) { |
| 696 | grpc_chttp2_schedule_closure(&t->global, op->on_consumed, 1); |
| 697 | } |
| 698 | |
| 699 | if (op->on_connectivity_state_change) { |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 700 | grpc_connectivity_state_notify_on_state_change( |
| 701 | &t->channel_callback.state_tracker, op->connectivity_state, |
| 702 | op->on_connectivity_state_change); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | if (op->send_goaway) { |
Craig Tiller | 9188d7a | 2015-07-05 12:44:37 -0700 | [diff] [blame] | 706 | t->global.sent_goaway = 1; |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 707 | grpc_chttp2_goaway_append( |
| 708 | t->global.last_incoming_stream_id, |
| 709 | grpc_chttp2_grpc_status_to_http2_error(op->goaway_status), |
Craig Tiller | bea1e62 | 2015-07-07 09:53:40 -0700 | [diff] [blame] | 710 | gpr_slice_ref(*op->goaway_message), &t->global.qbuf); |
Craig Tiller | 9188d7a | 2015-07-05 12:44:37 -0700 | [diff] [blame] | 711 | if (!grpc_chttp2_has_streams(t)) { |
| 712 | close_transport_locked(t); |
| 713 | } |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (op->set_accept_stream != NULL) { |
| 717 | t->channel_callback.accept_stream = op->set_accept_stream; |
| 718 | t->channel_callback.accept_stream_user_data = |
| 719 | op->set_accept_stream_user_data; |
| 720 | } |
| 721 | |
| 722 | if (op->bind_pollset) { |
| 723 | add_to_pollset_locked(t, op->bind_pollset); |
| 724 | } |
| 725 | |
| 726 | if (op->send_ping) { |
| 727 | send_ping_locked(t, op->send_ping); |
| 728 | } |
| 729 | |
| 730 | if (op->disconnect) { |
| 731 | close_transport_locked(t); |
| 732 | } |
| 733 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 734 | unlock(t); |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * INPUT PROCESSING |
| 739 | */ |
| 740 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 741 | static grpc_stream_state compute_state(gpr_uint8 write_closed, |
| 742 | gpr_uint8 read_closed) { |
| 743 | if (write_closed && read_closed) return GRPC_STREAM_CLOSED; |
| 744 | if (write_closed) return GRPC_STREAM_SEND_CLOSED; |
| 745 | if (read_closed) return GRPC_STREAM_RECV_CLOSED; |
| 746 | return GRPC_STREAM_OPEN; |
| 747 | } |
| 748 | |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 749 | static void remove_stream(grpc_chttp2_transport *t, gpr_uint32 id) { |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 750 | size_t new_stream_count; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 751 | grpc_chttp2_stream *s = |
| 752 | grpc_chttp2_stream_map_delete(&t->parsing_stream_map, id); |
Craig Tiller | c3fdaf9 | 2015-06-18 10:16:52 -0700 | [diff] [blame] | 753 | if (!s) { |
| 754 | s = grpc_chttp2_stream_map_delete(&t->new_stream_map, id); |
Craig Tiller | 48bfcdc | 2015-04-24 14:24:27 -0700 | [diff] [blame] | 755 | } |
Craig Tiller | 994c262 | 2015-07-23 14:00:58 -0700 | [diff] [blame] | 756 | grpc_chttp2_list_remove_writable_stream(&t->global, &s->global); |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 757 | GPR_ASSERT(s); |
| 758 | s->global.in_stream_map = 0; |
Craig Tiller | 66abdaa | 2015-06-17 08:00:39 -0700 | [diff] [blame] | 759 | if (t->parsing.incoming_stream == &s->parsing) { |
| 760 | t->parsing.incoming_stream = NULL; |
| 761 | grpc_chttp2_parsing_become_skip_parser(&t->parsing); |
| 762 | } |
Craig Tiller | 9188d7a | 2015-07-05 12:44:37 -0700 | [diff] [blame] | 763 | if (grpc_chttp2_unregister_stream(t, s) && t->global.sent_goaway) { |
| 764 | close_transport_locked(t); |
| 765 | } |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 766 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 767 | new_stream_count = grpc_chttp2_stream_map_size(&t->parsing_stream_map) + |
| 768 | grpc_chttp2_stream_map_size(&t->new_stream_map); |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 769 | if (new_stream_count != t->global.concurrent_stream_count) { |
| 770 | t->global.concurrent_stream_count = new_stream_count; |
| 771 | maybe_start_some_streams(&t->global); |
Craig Tiller | 59abfc2 | 2015-04-27 09:29:35 -0700 | [diff] [blame] | 772 | } |
Craig Tiller | 48bfcdc | 2015-04-24 14:24:27 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 775 | static void unlock_check_read_write_state(grpc_chttp2_transport *t) { |
| 776 | grpc_chttp2_transport_global *transport_global = &t->global; |
Craig Tiller | f73fcd1 | 2015-06-16 16:25:26 -0700 | [diff] [blame] | 777 | grpc_chttp2_stream_global *stream_global; |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 778 | grpc_stream_state state; |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 779 | |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 780 | if (!t->parsing_active) { |
Craig Tiller | 8823b14 | 2015-06-18 11:41:24 -0700 | [diff] [blame] | 781 | /* if a stream is in the stream map, and gets cancelled, we need to ensure |
| 782 | we are not parsing before continuing the cancellation to keep things in |
| 783 | a sane state */ |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 784 | while (grpc_chttp2_list_pop_closed_waiting_for_parsing(transport_global, |
| 785 | &stream_global)) { |
| 786 | GPR_ASSERT(stream_global->in_stream_map); |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 787 | GPR_ASSERT(stream_global->write_state != GRPC_WRITE_STATE_OPEN); |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 788 | GPR_ASSERT(stream_global->read_closed); |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 789 | remove_stream(t, stream_global->id); |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 790 | grpc_chttp2_list_add_read_write_state_changed(transport_global, |
| 791 | stream_global); |
| 792 | } |
| 793 | } |
| 794 | |
Craig Tiller | 17be5dc | 2015-07-01 10:36:27 -0700 | [diff] [blame] | 795 | if (!t->writing_active) { |
Craig Tiller | b367153 | 2015-07-01 10:37:40 -0700 | [diff] [blame] | 796 | while (grpc_chttp2_list_pop_cancelled_waiting_for_writing(transport_global, |
| 797 | &stream_global)) { |
| 798 | grpc_chttp2_list_add_read_write_state_changed(transport_global, |
| 799 | stream_global); |
Craig Tiller | 17be5dc | 2015-07-01 10:36:27 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 803 | while (grpc_chttp2_list_pop_read_write_state_changed(transport_global, |
| 804 | &stream_global)) { |
Craig Tiller | cec9eb9 | 2015-06-17 17:16:48 -0700 | [diff] [blame] | 805 | if (stream_global->cancelled) { |
Craig Tiller | b367153 | 2015-07-01 10:37:40 -0700 | [diff] [blame] | 806 | if (t->writing_active && |
| 807 | stream_global->write_state != GRPC_WRITE_STATE_SENT_CLOSE) { |
| 808 | grpc_chttp2_list_add_cancelled_waiting_for_writing(transport_global, |
| 809 | stream_global); |
Craig Tiller | 17be5dc | 2015-07-01 10:36:27 -0700 | [diff] [blame] | 810 | } else { |
| 811 | stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE; |
| 812 | stream_global->read_closed = 1; |
| 813 | if (!stream_global->published_cancelled) { |
| 814 | char buffer[GPR_LTOA_MIN_BUFSIZE]; |
| 815 | gpr_ltoa(stream_global->cancelled_status, buffer); |
| 816 | grpc_chttp2_incoming_metadata_buffer_add( |
| 817 | &stream_global->incoming_metadata, |
| 818 | grpc_mdelem_from_strings(t->metadata_context, "grpc-status", |
| 819 | buffer)); |
| 820 | grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into( |
| 821 | &stream_global->incoming_metadata, &stream_global->incoming_sopb); |
| 822 | stream_global->published_cancelled = 1; |
| 823 | } |
Craig Tiller | c1f7560 | 2015-04-24 11:44:53 -0700 | [diff] [blame] | 824 | } |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 825 | } |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 826 | if (stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE && |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 827 | stream_global->read_closed && stream_global->in_stream_map) { |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 828 | if (t->parsing_active) { |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 829 | grpc_chttp2_list_add_closed_waiting_for_parsing(transport_global, |
| 830 | stream_global); |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 831 | } else { |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 832 | remove_stream(t, stream_global->id); |
Craig Tiller | 48bfcdc | 2015-04-24 14:24:27 -0700 | [diff] [blame] | 833 | } |
Craig Tiller | 83fb070 | 2015-06-16 21:13:07 -0700 | [diff] [blame] | 834 | } |
Craig Tiller | cec9eb9 | 2015-06-17 17:16:48 -0700 | [diff] [blame] | 835 | if (!stream_global->publish_sopb) { |
Craig Tiller | cec9eb9 | 2015-06-17 17:16:48 -0700 | [diff] [blame] | 836 | continue; |
| 837 | } |
Craig Tiller | 994c262 | 2015-07-23 14:00:58 -0700 | [diff] [blame] | 838 | if (stream_global->writing_now) { |
| 839 | continue; |
| 840 | } |
Craig Tiller | eaa660e | 2015-06-18 16:36:12 -0700 | [diff] [blame] | 841 | /* FIXME(ctiller): we include in_stream_map in our computation of |
| 842 | whether the stream is write-closed. This is completely bogus, |
| 843 | but has the effect of delaying stream-closed until the stream |
| 844 | is indeed evicted from the stream map, making it safe to delete. |
| 845 | To fix this will require having an edge after stream-closed |
| 846 | indicating that the stream is closed AND safe to delete. */ |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 847 | state = compute_state( |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 848 | stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE && |
| 849 | !stream_global->in_stream_map, |
Craig Tiller | b951df1 | 2015-06-18 15:46:58 -0700 | [diff] [blame] | 850 | stream_global->read_closed); |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 851 | if (stream_global->incoming_sopb.nops == 0 && |
| 852 | state == stream_global->published_state) { |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 853 | continue; |
| 854 | } |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 855 | grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op( |
| 856 | &stream_global->incoming_metadata, &stream_global->incoming_sopb, |
| 857 | &stream_global->outstanding_metadata); |
Craig Tiller | f73fcd1 | 2015-06-16 16:25:26 -0700 | [diff] [blame] | 858 | grpc_sopb_swap(stream_global->publish_sopb, &stream_global->incoming_sopb); |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 859 | stream_global->published_state = *stream_global->publish_state = state; |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 860 | grpc_chttp2_schedule_closure(transport_global, |
| 861 | stream_global->recv_done_closure, 1); |
Craig Tiller | 31123db | 2015-06-16 17:06:31 -0700 | [diff] [blame] | 862 | stream_global->recv_done_closure = NULL; |
| 863 | stream_global->publish_sopb = NULL; |
| 864 | stream_global->publish_state = NULL; |
Craig Tiller | f73fcd1 | 2015-06-16 16:25:26 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 868 | static void cancel_from_api(grpc_chttp2_transport_global *transport_global, |
| 869 | grpc_chttp2_stream_global *stream_global, |
| 870 | grpc_status_code status) { |
Craig Tiller | b787c50 | 2015-06-15 16:14:52 -0700 | [diff] [blame] | 871 | stream_global->cancelled = 1; |
Craig Tiller | cec9eb9 | 2015-06-17 17:16:48 -0700 | [diff] [blame] | 872 | stream_global->cancelled_status = status; |
| 873 | if (stream_global->id != 0) { |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 874 | gpr_slice_buffer_add( |
| 875 | &transport_global->qbuf, |
| 876 | grpc_chttp2_rst_stream_create( |
| 877 | stream_global->id, grpc_chttp2_grpc_status_to_http2_error(status))); |
Craig Tiller | b787c50 | 2015-06-15 16:14:52 -0700 | [diff] [blame] | 878 | } |
Craig Tiller | cec9eb9 | 2015-06-17 17:16:48 -0700 | [diff] [blame] | 879 | grpc_chttp2_list_add_read_write_state_changed(transport_global, |
| 880 | stream_global); |
Craig Tiller | b787c50 | 2015-06-15 16:14:52 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 883 | static void cancel_stream_cb(grpc_chttp2_transport_global *transport_global, |
| 884 | void *user_data, |
| 885 | grpc_chttp2_stream_global *stream_global) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 886 | cancel_from_api(transport_global, stream_global, GRPC_STATUS_UNAVAILABLE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 889 | static void end_all_the_calls(grpc_chttp2_transport *t) { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 890 | grpc_chttp2_for_all_streams(&t->global, NULL, cancel_stream_cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 893 | static void drop_connection(grpc_chttp2_transport *t) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 894 | close_transport_locked(t); |
| 895 | end_all_the_calls(t); |
| 896 | } |
| 897 | |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 898 | /** update window from a settings change */ |
| 899 | static void update_global_window(void *args, gpr_uint32 id, void *stream) { |
| 900 | grpc_chttp2_transport *t = args; |
| 901 | grpc_chttp2_stream *s = stream; |
| 902 | grpc_chttp2_transport_global *transport_global = &t->global; |
| 903 | grpc_chttp2_stream_global *stream_global = &s->global; |
Craig Tiller | 6f7411f | 2015-07-06 15:37:28 -0700 | [diff] [blame] | 904 | int was_zero; |
| 905 | int is_zero; |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 906 | |
| 907 | GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("settings", transport_global, stream_global, |
| 908 | outgoing_window, |
| 909 | t->parsing.initial_window_update); |
Craig Tiller | 6f7411f | 2015-07-06 15:37:28 -0700 | [diff] [blame] | 910 | was_zero = stream_global->outgoing_window <= 0; |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 911 | stream_global->outgoing_window += t->parsing.initial_window_update; |
Craig Tiller | 6f7411f | 2015-07-06 15:37:28 -0700 | [diff] [blame] | 912 | is_zero = stream_global->outgoing_window <= 0; |
| 913 | |
| 914 | if (was_zero && !is_zero) { |
| 915 | grpc_chttp2_list_add_writable_stream(transport_global, stream_global); |
| 916 | } |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 919 | static void read_error_locked(grpc_chttp2_transport *t) { |
| 920 | t->endpoint_reading = 0; |
| 921 | if (!t->writing_active && t->ep) { |
| 922 | grpc_endpoint_destroy(t->ep); |
| 923 | t->ep = NULL; |
| 924 | /* safe as we still have a ref for read */ |
| 925 | UNREF_TRANSPORT(t, "disconnect"); |
| 926 | } |
| 927 | } |
| 928 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 929 | /* tcp read callback */ |
| 930 | static void recv_data(void *tp, gpr_slice *slices, size_t nslices, |
| 931 | grpc_endpoint_cb_status error) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 932 | grpc_chttp2_transport *t = tp; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 933 | size_t i; |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 934 | int unref = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 935 | |
| 936 | switch (error) { |
| 937 | case GRPC_ENDPOINT_CB_SHUTDOWN: |
| 938 | case GRPC_ENDPOINT_CB_EOF: |
| 939 | case GRPC_ENDPOINT_CB_ERROR: |
| 940 | lock(t); |
| 941 | drop_connection(t); |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 942 | read_error_locked(t); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 943 | unlock(t); |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 944 | unref = 1; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 945 | for (i = 0; i < nslices; i++) gpr_slice_unref(slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 946 | break; |
| 947 | case GRPC_ENDPOINT_CB_OK: |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 948 | lock(t); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 949 | i = 0; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 950 | GPR_ASSERT(!t->parsing_active); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 951 | if (!t->closed) { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 952 | t->parsing_active = 1; |
Craig Tiller | 759eb32 | 2015-06-16 22:41:18 -0700 | [diff] [blame] | 953 | /* merge stream lists */ |
| 954 | grpc_chttp2_stream_map_move_into(&t->new_stream_map, |
| 955 | &t->parsing_stream_map); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 956 | grpc_chttp2_prepare_to_read(&t->global, &t->parsing); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 957 | gpr_mu_unlock(&t->mu); |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 958 | for (; i < nslices && grpc_chttp2_perform_read(&t->parsing, slices[i]); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 959 | i++) { |
| 960 | gpr_slice_unref(slices[i]); |
| 961 | } |
Craig Tiller | 02f254e | 2015-06-11 23:06:07 -0700 | [diff] [blame] | 962 | gpr_mu_lock(&t->mu); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 963 | if (i != nslices) { |
| 964 | drop_connection(t); |
| 965 | } |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 966 | /* merge stream lists */ |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 967 | grpc_chttp2_stream_map_move_into(&t->new_stream_map, |
| 968 | &t->parsing_stream_map); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 969 | t->global.concurrent_stream_count = |
| 970 | grpc_chttp2_stream_map_size(&t->parsing_stream_map); |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 971 | if (t->parsing.initial_window_update != 0) { |
| 972 | grpc_chttp2_stream_map_for_each(&t->parsing_stream_map, |
| 973 | update_global_window, t); |
Craig Tiller | 0e640a8 | 2015-07-01 15:41:52 -0700 | [diff] [blame] | 974 | t->parsing.initial_window_update = 0; |
Craig Tiller | ab63073 | 2015-06-25 11:20:01 -0700 | [diff] [blame] | 975 | } |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 976 | /* handle higher level things */ |
| 977 | grpc_chttp2_publish_reads(&t->global, &t->parsing); |
| 978 | t->parsing_active = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 979 | } |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 980 | if (i == nslices) { |
Craig Tiller | 8b2f1d7 | 2015-06-18 13:38:38 -0700 | [diff] [blame] | 981 | grpc_chttp2_schedule_closure(&t->global, &t->reading_action, 1); |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 982 | } else { |
| 983 | read_error_locked(t); |
| 984 | unref = 1; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 985 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 986 | unlock(t); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 987 | for (; i < nslices; i++) gpr_slice_unref(slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 988 | break; |
| 989 | } |
Craig Tiller | 69f5616 | 2015-06-30 14:54:31 -0700 | [diff] [blame] | 990 | if (unref) { |
| 991 | UNREF_TRANSPORT(t, "recv_data"); |
| 992 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 993 | } |
| 994 | |
Craig Tiller | 8b2f1d7 | 2015-06-18 13:38:38 -0700 | [diff] [blame] | 995 | static void reading_action(void *pt, int iomgr_success_ignored) { |
| 996 | grpc_chttp2_transport *t = pt; |
| 997 | grpc_endpoint_notify_on_read(t->ep, recv_data, t); |
| 998 | } |
| 999 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1000 | /* |
| 1001 | * CALLBACK LOOP |
| 1002 | */ |
| 1003 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 1004 | static void schedule_closure_for_connectivity(void *a, |
| 1005 | grpc_iomgr_closure *closure) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1006 | grpc_chttp2_schedule_closure(a, closure, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 1009 | static void connectivity_state_set( |
| 1010 | grpc_chttp2_transport_global *transport_global, |
| 1011 | grpc_connectivity_state state) { |
| 1012 | GRPC_CHTTP2_IF_TRACING( |
| 1013 | gpr_log(GPR_DEBUG, "set connectivity_state=%d", state)); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1014 | grpc_connectivity_state_set_with_scheduler( |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 1015 | &TRANSPORT_FROM_GLOBAL(transport_global)->channel_callback.state_tracker, |
| 1016 | state, schedule_closure_for_connectivity, transport_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 1019 | void grpc_chttp2_schedule_closure( |
| 1020 | grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure, |
| 1021 | int success) { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 1022 | closure->success = success; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1023 | if (transport_global->pending_closures_tail == NULL) { |
| 1024 | transport_global->pending_closures_head = |
| 1025 | transport_global->pending_closures_tail = closure; |
| 1026 | } else { |
| 1027 | transport_global->pending_closures_tail->next = closure; |
| 1028 | transport_global->pending_closures_tail = closure; |
| 1029 | } |
| 1030 | closure->next = NULL; |
Craig Tiller | 748fe3f | 2015-03-02 07:48:50 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 1033 | /* |
| 1034 | * POLLSET STUFF |
| 1035 | */ |
| 1036 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 1037 | static void add_to_pollset_locked(grpc_chttp2_transport *t, |
| 1038 | grpc_pollset *pollset) { |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 1039 | if (t->ep) { |
| 1040 | grpc_endpoint_add_to_pollset(t->ep, pollset); |
| 1041 | } |
Craig Tiller | c079c11 | 2015-04-22 15:23:39 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1044 | /* |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 1045 | * TRACING |
| 1046 | */ |
| 1047 | |
| 1048 | void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason, |
| 1049 | const char *context, const char *var, |
| 1050 | int is_client, gpr_uint32 stream_id, |
| 1051 | gpr_int64 current_value, gpr_int64 delta) { |
| 1052 | char *identifier; |
| 1053 | char *context_scope; |
| 1054 | char *context_thread; |
| 1055 | char *underscore_pos = strchr(context, '_'); |
| 1056 | GPR_ASSERT(underscore_pos); |
| 1057 | context_thread = gpr_strdup(underscore_pos + 1); |
| 1058 | context_scope = gpr_strdup(context); |
| 1059 | context_scope[underscore_pos - context] = 0; |
| 1060 | if (stream_id) { |
| 1061 | gpr_asprintf(&identifier, "%s[%d]", context_scope, stream_id); |
| 1062 | } else { |
| 1063 | identifier = gpr_strdup(context_scope); |
| 1064 | } |
Craig Tiller | 8b2f1d7 | 2015-06-18 13:38:38 -0700 | [diff] [blame] | 1065 | gpr_log(GPR_INFO, |
Craig Tiller | 5c517b1 | 2015-07-15 13:28:10 -0700 | [diff] [blame] | 1066 | "FLOWCTL: %s %-10s %8s %-27s %8lld %c %8lld = %8lld %-10s [%s:%d]", |
Craig Tiller | 285b882 | 2015-06-17 15:58:13 -0700 | [diff] [blame] | 1067 | is_client ? "client" : "server", identifier, context_thread, var, |
| 1068 | current_value, delta < 0 ? '-' : '+', delta < 0 ? -delta : delta, |
| 1069 | current_value + delta, reason, file, line); |
| 1070 | gpr_free(identifier); |
| 1071 | gpr_free(context_thread); |
| 1072 | gpr_free(context_scope); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1076 | * INTEGRATION GLUE |
| 1077 | */ |
| 1078 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 1079 | static char *chttp2_get_peer(grpc_transport *t) { |
| 1080 | return gpr_strdup(((grpc_chttp2_transport *)t)->peer_string); |
| 1081 | } |
| 1082 | |
| 1083 | static const grpc_transport_vtable vtable = {sizeof(grpc_chttp2_stream), |
| 1084 | init_stream, |
| 1085 | perform_stream_op, |
| 1086 | perform_transport_op, |
| 1087 | destroy_stream, |
| 1088 | destroy_transport, |
| 1089 | chttp2_get_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1090 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 1091 | grpc_transport *grpc_create_chttp2_transport( |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 1092 | const grpc_channel_args *channel_args, grpc_endpoint *ep, grpc_mdctx *mdctx, |
| 1093 | int is_client) { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 1094 | grpc_chttp2_transport *t = gpr_malloc(sizeof(grpc_chttp2_transport)); |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 1095 | init_transport(t, channel_args, ep, mdctx, is_client); |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 1096 | return &t->base; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1097 | } |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 1098 | |
| 1099 | void grpc_chttp2_transport_start_reading(grpc_transport *transport, |
| 1100 | gpr_slice *slices, size_t nslices) { |
| 1101 | grpc_chttp2_transport *t = (grpc_chttp2_transport *)transport; |
| 1102 | REF_TRANSPORT(t, "recv_data"); /* matches unref inside recv_data */ |
| 1103 | recv_data(t, slices, nslices, GRPC_ENDPOINT_CB_OK); |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 1104 | } |