Craig Tiller | af69180 | 2015-06-23 14:57:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/client_config/subchannel.h" |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 35 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 40 | #include "src/core/channel/channel_args.h" |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 41 | #include "src/core/channel/connected_channel.h" |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 42 | #include "src/core/channel/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | |
| 44 | typedef struct { |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 45 | /* all fields protected by subchannel->mu */ |
| 46 | /** refcount */ |
| 47 | int refs; |
| 48 | /** parent subchannel */ |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 49 | grpc_subchannel *subchannel; |
| 50 | } connection; |
| 51 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 52 | typedef struct waiting_for_connect { |
| 53 | struct waiting_for_connect *next; |
| 54 | grpc_iomgr_closure *notify; |
| 55 | grpc_transport_stream_op *initial_op; |
| 56 | grpc_subchannel_call **target; |
| 57 | } waiting_for_connect; |
| 58 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 59 | struct grpc_subchannel { |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 60 | grpc_connector *connector; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 61 | |
| 62 | /** non-transport related channel filters */ |
| 63 | const grpc_channel_filter **filters; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 64 | size_t num_filters; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 65 | /** channel arguments */ |
| 66 | grpc_channel_args *args; |
| 67 | /** address to connect to */ |
| 68 | struct sockaddr *addr; |
| 69 | size_t addr_len; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 70 | /** metadata context */ |
| 71 | grpc_mdctx *mdctx; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 72 | |
| 73 | /** set during connection */ |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 74 | grpc_connect_out_args connecting_result; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 75 | |
| 76 | /** callback for connection finishing */ |
| 77 | grpc_iomgr_closure connected; |
| 78 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 79 | /** pollset_set tracking who's interested in a connection |
| 80 | being setup */ |
| 81 | grpc_pollset_set pollset_set; |
| 82 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 83 | /** mutex protecting remaining elements */ |
| 84 | gpr_mu mu; |
| 85 | |
| 86 | /** active connection */ |
| 87 | connection *active; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 88 | /** refcount */ |
| 89 | int refs; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 90 | /** are we connecting */ |
| 91 | int connecting; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 92 | /** things waiting for a connection */ |
| 93 | waiting_for_connect *waiting; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 94 | /** connectivity state tracking */ |
| 95 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct grpc_subchannel_call { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 99 | connection *connection; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 100 | gpr_refcount refs; |
| 101 | }; |
| 102 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 103 | #define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1)) |
| 104 | #define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1)) |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 105 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 106 | static grpc_subchannel_call *create_call(connection *con, grpc_transport_stream_op *initial_op); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 107 | static void connectivity_state_changed_locked(grpc_subchannel *c); |
| 108 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c); |
| 109 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 110 | static void subchannel_connected(void *subchannel, int iomgr_success); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 111 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 112 | static void subchannel_ref_locked(grpc_subchannel *c); |
| 113 | static int subchannel_unref_locked(grpc_subchannel *c) GRPC_MUST_USE_RESULT; |
| 114 | static void connection_ref_locked(connection *c); |
| 115 | static grpc_subchannel *connection_unref_locked(connection *c) GRPC_MUST_USE_RESULT; |
| 116 | static void subchannel_destroy(grpc_subchannel *c); |
| 117 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 118 | /* |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 119 | * connection implementation |
| 120 | */ |
| 121 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 122 | static void connection_destroy(connection *c) { |
| 123 | GPR_ASSERT(c->refs == 0); |
| 124 | grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CONNECTION(c)); |
| 125 | gpr_free(c); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 128 | static void connection_ref_locked(connection *c) { |
| 129 | subchannel_ref_locked(c->subchannel); |
| 130 | ++c->refs; |
| 131 | } |
| 132 | |
| 133 | static grpc_subchannel *connection_unref_locked(connection *c) { |
| 134 | grpc_subchannel *destroy = NULL; |
| 135 | if (subchannel_unref_locked(c->subchannel)) { |
| 136 | destroy = c->subchannel; |
| 137 | } |
| 138 | if (--c->refs == 0 && c->subchannel->active != c) { |
| 139 | connection_destroy(c); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 140 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 141 | return destroy; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 144 | |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 145 | /* |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 146 | * grpc_subchannel implementation |
| 147 | */ |
| 148 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 149 | static void subchannel_ref_locked(grpc_subchannel *c) { |
| 150 | ++c->refs; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 151 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 152 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 153 | static int subchannel_unref_locked(grpc_subchannel *c) { |
| 154 | return --c->refs == 0; |
| 155 | } |
| 156 | |
| 157 | void grpc_subchannel_ref(grpc_subchannel *c) { |
| 158 | gpr_mu_lock(&c->mu); |
| 159 | subchannel_ref_locked(c); |
| 160 | gpr_mu_unlock(&c->mu); |
| 161 | } |
| 162 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 163 | void grpc_subchannel_unref(grpc_subchannel *c) { |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 164 | int destroy; |
| 165 | gpr_mu_lock(&c->mu); |
| 166 | destroy = subchannel_unref_locked(c); |
| 167 | gpr_mu_unlock(&c->mu); |
| 168 | if (destroy) subchannel_destroy(c); |
| 169 | } |
| 170 | |
| 171 | static void subchannel_destroy(grpc_subchannel *c) { |
| 172 | if (c->active != NULL) { |
| 173 | connection_destroy(c->active); |
| 174 | } |
| 175 | gpr_free(c->filters); |
| 176 | grpc_channel_args_destroy(c->args); |
| 177 | gpr_free(c->addr); |
| 178 | grpc_mdctx_unref(c->mdctx); |
| 179 | grpc_pollset_set_destroy(&c->pollset_set); |
| 180 | grpc_connectivity_state_destroy(&c->state_tracker); |
| 181 | gpr_free(c); |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 184 | void grpc_subchannel_add_interested_party(grpc_subchannel *c, |
| 185 | grpc_pollset *pollset) { |
| 186 | grpc_pollset_set_add_pollset(&c->pollset_set, pollset); |
| 187 | } |
| 188 | |
| 189 | void grpc_subchannel_del_interested_party(grpc_subchannel *c, |
| 190 | grpc_pollset *pollset) { |
| 191 | grpc_pollset_set_del_pollset(&c->pollset_set, pollset); |
| 192 | } |
| 193 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 194 | grpc_subchannel *grpc_subchannel_create(grpc_connector *connector, |
| 195 | grpc_subchannel_args *args) { |
| 196 | grpc_subchannel *c = gpr_malloc(sizeof(*c)); |
| 197 | memset(c, 0, sizeof(*c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 198 | c->refs = 1; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 199 | c->connector = connector; |
| 200 | grpc_connector_ref(c->connector); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 201 | c->num_filters = args->filter_count; |
| 202 | c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 203 | memcpy(c->filters, args->filters, |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 204 | sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 205 | c->addr = gpr_malloc(args->addr_len); |
| 206 | memcpy(c->addr, args->addr, args->addr_len); |
| 207 | c->addr_len = args->addr_len; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 208 | c->args = grpc_channel_args_copy(args->args); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 209 | c->mdctx = args->mdctx; |
| 210 | grpc_mdctx_ref(c->mdctx); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 211 | grpc_pollset_set_init(&c->pollset_set); |
| 212 | grpc_iomgr_closure_init(&c->connected, subchannel_connected, c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 213 | grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 214 | gpr_mu_init(&c->mu); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 215 | return c; |
| 216 | } |
| 217 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 218 | static void start_connect(grpc_subchannel *c) { |
| 219 | grpc_connect_in_args args; |
| 220 | |
| 221 | args.interested_parties = &c->pollset_set; |
| 222 | args.addr = c->addr; |
| 223 | args.addr_len = c->addr_len; |
| 224 | args.deadline = compute_connect_deadline(c); |
| 225 | args.channel_args = c->args; |
| 226 | args.metadata_context = c->mdctx; |
| 227 | |
| 228 | grpc_connector_connect(c->connector, &args, &c->connecting_result, &c->connected); |
| 229 | } |
| 230 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 231 | void grpc_subchannel_create_call(grpc_subchannel *c, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 232 | grpc_transport_stream_op *initial_op, |
| 233 | grpc_subchannel_call **target, |
| 234 | grpc_iomgr_closure *notify) { |
| 235 | connection *con; |
| 236 | gpr_mu_lock(&c->mu); |
| 237 | if (c->active != NULL) { |
| 238 | con = c->active; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 239 | connection_ref_locked(con); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 240 | gpr_mu_unlock(&c->mu); |
| 241 | |
| 242 | *target = create_call(con, initial_op); |
| 243 | notify->cb(notify->cb_arg, 1); |
| 244 | } else { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 245 | waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c)); |
| 246 | w4c->next = c->waiting; |
| 247 | w4c->notify = notify; |
| 248 | w4c->initial_op = initial_op; |
| 249 | w4c->target = target; |
| 250 | c->waiting = w4c; |
| 251 | grpc_subchannel_add_interested_party(c, initial_op->bind_pollset); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 252 | if (!c->connecting) { |
| 253 | c->connecting = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 254 | connectivity_state_changed_locked(c); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 255 | subchannel_ref_locked(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 256 | gpr_mu_unlock(&c->mu); |
| 257 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 258 | start_connect(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 259 | } else { |
| 260 | gpr_mu_unlock(&c->mu); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 265 | grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) { |
| 266 | grpc_connectivity_state state; |
| 267 | gpr_mu_lock(&c->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 268 | state = grpc_connectivity_state_check(&c->state_tracker); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 269 | gpr_mu_unlock(&c->mu); |
| 270 | return state; |
| 271 | } |
| 272 | |
| 273 | void grpc_subchannel_notify_on_state_change(grpc_subchannel *c, |
| 274 | grpc_connectivity_state *state, |
| 275 | grpc_iomgr_closure *notify) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 276 | int do_connect = 0; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 277 | gpr_mu_lock(&c->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 278 | if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state, notify)) { |
| 279 | do_connect = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 280 | c->connecting = 1; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 281 | subchannel_ref_locked(c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 282 | grpc_connectivity_state_set(&c->state_tracker, compute_connectivity_locked(c)); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 283 | } |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 284 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 285 | if (do_connect) { |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 286 | start_connect(c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 290 | void grpc_subchannel_process_transport_op(grpc_subchannel *c, grpc_transport_op *op) { |
| 291 | abort(); |
| 292 | } |
| 293 | |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 294 | static void publish_transport(grpc_subchannel *c) { |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 295 | size_t channel_stack_size; |
| 296 | connection *con; |
| 297 | grpc_channel_stack *stk; |
| 298 | size_t num_filters; |
| 299 | const grpc_channel_filter **filters; |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 300 | waiting_for_connect *w4c; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 301 | int destroy; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 302 | |
| 303 | num_filters = c->num_filters + c->connecting_result.num_filters + 1; |
| 304 | filters = gpr_malloc(sizeof(*filters) * num_filters); |
| 305 | memcpy(filters, c->filters, sizeof(*filters) * c->num_filters); |
| 306 | memcpy(filters + c->num_filters, c->connecting_result.filters, sizeof(*filters) * c->connecting_result.num_filters); |
| 307 | filters[num_filters - 1] = &grpc_connected_channel_filter; |
| 308 | |
| 309 | channel_stack_size = grpc_channel_stack_size(filters, num_filters); |
| 310 | con = gpr_malloc(sizeof(connection) + channel_stack_size); |
| 311 | stk = (grpc_channel_stack *)(con + 1); |
| 312 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 313 | con->refs = 0; |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 314 | con->subchannel = c; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 315 | grpc_channel_stack_init(filters, num_filters, c->args, c->mdctx, stk); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 316 | grpc_connected_channel_bind_transport(stk, c->connecting_result.transport); |
| 317 | memset(&c->connecting_result, 0, sizeof(c->connecting_result)); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 318 | |
| 319 | gpr_mu_lock(&c->mu); |
| 320 | GPR_ASSERT(c->active == NULL); |
| 321 | c->active = con; |
| 322 | c->connecting = 0; |
| 323 | connectivity_state_changed_locked(c); |
| 324 | while ((w4c = c->waiting)) { |
| 325 | abort(); /* not implemented */ |
| 326 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 327 | destroy = subchannel_unref_locked(c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 328 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 329 | |
| 330 | gpr_free(filters); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 331 | |
| 332 | if (destroy) { |
| 333 | subchannel_destroy(c); |
| 334 | } |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void subchannel_connected(void *arg, int iomgr_success) { |
| 338 | grpc_subchannel *c = arg; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 339 | if (c->connecting_result.transport) { |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 340 | publish_transport(c); |
| 341 | } else { |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 342 | int destroy; |
| 343 | gpr_mu_lock(&c->mu); |
| 344 | destroy = subchannel_unref_locked(c); |
| 345 | gpr_mu_unlock(&c->mu); |
| 346 | if (destroy) subchannel_destroy(c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 347 | /* TODO(ctiller): retry after sleeping */ |
| 348 | abort(); |
| 349 | } |
| 350 | } |
| 351 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 352 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c) { |
| 353 | return gpr_time_add(gpr_now(), gpr_time_from_seconds(60)); |
| 354 | } |
| 355 | |
| 356 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) { |
| 357 | if (c->connecting) { |
| 358 | return GRPC_CHANNEL_CONNECTING; |
| 359 | } |
| 360 | if (c->active) { |
| 361 | return GRPC_CHANNEL_READY; |
| 362 | } |
| 363 | return GRPC_CHANNEL_IDLE; |
| 364 | } |
| 365 | |
| 366 | static void connectivity_state_changed_locked(grpc_subchannel *c) { |
| 367 | grpc_connectivity_state current = compute_connectivity_locked(c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 368 | grpc_connectivity_state_set(&c->state_tracker, current); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 371 | /* |
| 372 | * grpc_subchannel_call implementation |
| 373 | */ |
| 374 | |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 375 | void grpc_subchannel_call_ref(grpc_subchannel_call *c) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 376 | gpr_ref(&c->refs); |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 379 | void grpc_subchannel_call_unref(grpc_subchannel_call *c) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 380 | if (gpr_unref(&c->refs)) { |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 381 | gpr_mu *mu = &c->connection->subchannel->mu; |
| 382 | grpc_subchannel *destroy; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 383 | grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 384 | gpr_mu_lock(mu); |
| 385 | destroy = connection_unref_locked(c->connection); |
| 386 | gpr_mu_unlock(mu); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 387 | gpr_free(c); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame^] | 388 | if (destroy) { |
| 389 | subchannel_destroy(destroy); |
| 390 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | void grpc_subchannel_call_process_op(grpc_subchannel_call *call, |
| 395 | grpc_transport_stream_op *op) { |
| 396 | grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 397 | grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0); |
| 398 | top_elem->filter->start_transport_stream_op(top_elem, op); |
| 399 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 400 | |
| 401 | grpc_subchannel_call *create_call(connection *con, grpc_transport_stream_op *initial_op) { |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 402 | grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con); |
| 403 | grpc_subchannel_call *call = gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size); |
| 404 | grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 405 | call->connection = con; |
| 406 | gpr_ref_init(&call->refs, 1); |
| 407 | grpc_call_stack_init(chanstk, NULL, initial_op, callstk); |
| 408 | return call; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 409 | } |