blob: eadeb0ef5588ef124588a17515df4f43b01d956a [file] [log] [blame]
Craig Tilleraf691802015-06-23 14:57:07 -07001/*
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 Tiller2595ab72015-06-25 15:26:00 -070035
Craig Tillerf7afa1f2015-06-26 09:02:20 -070036#include <string.h>
37
Craig Tiller2595ab72015-06-25 15:26:00 -070038#include <grpc/support/alloc.h>
39
Craig Tillereb3b12e2015-06-26 14:42:49 -070040#include "src/core/channel/channel_args.h"
Craig Tillerff54c922015-06-26 16:57:20 -070041#include "src/core/channel/connected_channel.h"
Craig Tiller08a1cf82015-06-29 09:37:52 -070042#include "src/core/transport/connectivity_state.h"
Craig Tillereb3b12e2015-06-26 14:42:49 -070043
44typedef struct {
Craig Tiller4ab82d22015-06-29 09:40:33 -070045 /* all fields protected by subchannel->mu */
46 /** refcount */
47 int refs;
48 /** parent subchannel */
Craig Tillereb3b12e2015-06-26 14:42:49 -070049 grpc_subchannel *subchannel;
50} connection;
51
Craig Tillerdf91ba52015-06-29 10:55:46 -070052typedef struct {
53 grpc_iomgr_closure closure;
54 size_t version;
55 grpc_subchannel *subchannel;
56 grpc_connectivity_state connectivity_state;
57} state_watcher;
58
Craig Tiller5f84c842015-06-26 16:08:21 -070059typedef struct waiting_for_connect {
60 struct waiting_for_connect *next;
61 grpc_iomgr_closure *notify;
Craig Tillerdf91ba52015-06-29 10:55:46 -070062 grpc_transport_stream_op initial_op;
Craig Tiller5f84c842015-06-26 16:08:21 -070063 grpc_subchannel_call **target;
Craig Tillerdf91ba52015-06-29 10:55:46 -070064 grpc_subchannel *subchannel;
65 grpc_iomgr_closure continuation;
Craig Tiller5f84c842015-06-26 16:08:21 -070066} waiting_for_connect;
67
Craig Tiller2595ab72015-06-25 15:26:00 -070068struct grpc_subchannel {
Craig Tiller91624662015-06-25 16:31:02 -070069 grpc_connector *connector;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070070
71 /** non-transport related channel filters */
72 const grpc_channel_filter **filters;
Craig Tiller5945ee12015-06-27 10:36:09 -070073 size_t num_filters;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070074 /** channel arguments */
75 grpc_channel_args *args;
76 /** address to connect to */
77 struct sockaddr *addr;
78 size_t addr_len;
Craig Tiller5f84c842015-06-26 16:08:21 -070079 /** metadata context */
80 grpc_mdctx *mdctx;
Craig Tiller98465032015-06-29 14:36:42 -070081 /** master channel */
82 grpc_channel *master;
Craig Tillerb6fbf1d2015-06-29 15:25:49 -070083 /** have we seen a disconnection? */
84 int disconnected;
Craig Tillereb3b12e2015-06-26 14:42:49 -070085
86 /** set during connection */
Craig Tiller04c5d4b2015-06-26 17:21:41 -070087 grpc_connect_out_args connecting_result;
Craig Tillereb3b12e2015-06-26 14:42:49 -070088
89 /** callback for connection finishing */
90 grpc_iomgr_closure connected;
91
Craig Tiller5f84c842015-06-26 16:08:21 -070092 /** pollset_set tracking who's interested in a connection
93 being setup */
94 grpc_pollset_set pollset_set;
95
Craig Tillereb3b12e2015-06-26 14:42:49 -070096 /** mutex protecting remaining elements */
97 gpr_mu mu;
98
99 /** active connection */
100 connection *active;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700101 /** version number for the active connection */
102 size_t active_version;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700103 /** refcount */
104 int refs;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700105 /** are we connecting */
106 int connecting;
Craig Tiller5f84c842015-06-26 16:08:21 -0700107 /** things waiting for a connection */
108 waiting_for_connect *waiting;
Craig Tillerc7b5f762015-06-27 11:48:42 -0700109 /** connectivity state tracking */
110 grpc_connectivity_state_tracker state_tracker;
Craig Tiller2595ab72015-06-25 15:26:00 -0700111};
112
113struct grpc_subchannel_call {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700114 connection *connection;
Craig Tiller2595ab72015-06-25 15:26:00 -0700115 gpr_refcount refs;
116};
117
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700118#define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1))
119#define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1))
Craig Tiller2595ab72015-06-25 15:26:00 -0700120
Craig Tiller4ab82d22015-06-29 09:40:33 -0700121static grpc_subchannel_call *create_call(connection *con,
122 grpc_transport_stream_op *initial_op);
Craig Tiller5f84c842015-06-26 16:08:21 -0700123static void connectivity_state_changed_locked(grpc_subchannel *c);
124static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c);
125static gpr_timespec compute_connect_deadline(grpc_subchannel *c);
Craig Tillerff54c922015-06-26 16:57:20 -0700126static void subchannel_connected(void *subchannel, int iomgr_success);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700127
Craig Tillerc3967532015-06-29 14:59:38 -0700128static void subchannel_ref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
129static int subchannel_unref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT;
130static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
131static grpc_subchannel *connection_unref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
Craig Tiller4ab82d22015-06-29 09:40:33 -0700132 GRPC_MUST_USE_RESULT;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700133static void subchannel_destroy(grpc_subchannel *c);
134
Craig Tillerc3967532015-06-29 14:59:38 -0700135#ifdef GRPC_SUBCHANNEL_REFCOUNT_DEBUG
136#define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p), __FILE__, __LINE__, (r))
137#define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p), __FILE__, __LINE__, (r))
138#define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p), __FILE__, __LINE__, (r))
139#define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p), __FILE__, __LINE__, (r))
140#define REF_PASS_ARGS , file, line, reason
141#define REF_LOG(name, p) gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "%s: %p ref %d -> %d %s", (name), (p), (p)->refs, (p)->refs + 1, reason)
142#define UNREF_LOG(name, p) gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "%s: %p unref %d -> %d %s", (name), (p), (p)->refs, (p)->refs - 1, reason)
143#else
144#define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p))
145#define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p))
146#define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p), __FILE__, __LINE__, (r))
147#define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p), __FILE__, __LINE__, (r))
148#define REF_PASS_ARGS
149#endif
150
Craig Tiller2595ab72015-06-25 15:26:00 -0700151/*
Craig Tillerca3e9d32015-06-27 18:37:27 -0700152 * connection implementation
153 */
154
Craig Tillerd7b68e72015-06-28 11:41:09 -0700155static void connection_destroy(connection *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700156 GPR_ASSERT(c->refs == 0);
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700157 gpr_log(GPR_DEBUG, "CONNECTION_DESTROY %p", c);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700158 grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CONNECTION(c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700159 gpr_free(c);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700160}
161
Craig Tillerc3967532015-06-29 14:59:38 -0700162static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
163 REF_LOG("CONNECTION", c);
164 subchannel_ref_locked(c->subchannel REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700165 ++c->refs;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700166}
167
Craig Tillerc3967532015-06-29 14:59:38 -0700168static grpc_subchannel *connection_unref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700169 grpc_subchannel *destroy = NULL;
Craig Tillerc3967532015-06-29 14:59:38 -0700170 UNREF_LOG("CONNECTION", c);
171 if (subchannel_unref_locked(c->subchannel REF_PASS_ARGS)) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700172 destroy = c->subchannel;
173 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700174 if (--c->refs == 0 && c->subchannel->active != c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700175 connection_destroy(c);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700176 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700177 return destroy;
Craig Tillerca3e9d32015-06-27 18:37:27 -0700178}
179
180/*
Craig Tiller2595ab72015-06-25 15:26:00 -0700181 * grpc_subchannel implementation
182 */
183
Craig Tillerc3967532015-06-29 14:59:38 -0700184static void subchannel_ref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
185 REF_LOG("SUBCHANNEL", c);
186 ++c->refs;
187}
Craig Tiller2595ab72015-06-25 15:26:00 -0700188
Craig Tillerc3967532015-06-29 14:59:38 -0700189static int subchannel_unref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
190 UNREF_LOG("SUBCHANNEL", c);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700191 return --c->refs == 0;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700192}
193
Craig Tillerc3967532015-06-29 14:59:38 -0700194void grpc_subchannel_ref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700195 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700196 subchannel_ref_locked(c REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700197 gpr_mu_unlock(&c->mu);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700198}
199
Craig Tillerc3967532015-06-29 14:59:38 -0700200void grpc_subchannel_unref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700201 int destroy;
202 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700203 destroy = subchannel_unref_locked(c REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700204 gpr_mu_unlock(&c->mu);
205 if (destroy) subchannel_destroy(c);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700206}
207
208static void subchannel_destroy(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700209 if (c->active != NULL) {
210 connection_destroy(c->active);
211 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700212 gpr_free(c->filters);
213 grpc_channel_args_destroy(c->args);
214 gpr_free(c->addr);
215 grpc_mdctx_unref(c->mdctx);
216 grpc_pollset_set_destroy(&c->pollset_set);
217 grpc_connectivity_state_destroy(&c->state_tracker);
Craig Tillerc3967532015-06-29 14:59:38 -0700218 grpc_connector_unref(c->connector);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700219 gpr_free(c);
Craig Tiller2595ab72015-06-25 15:26:00 -0700220}
221
Craig Tiller5f84c842015-06-26 16:08:21 -0700222void grpc_subchannel_add_interested_party(grpc_subchannel *c,
223 grpc_pollset *pollset) {
224 grpc_pollset_set_add_pollset(&c->pollset_set, pollset);
225}
226
227void grpc_subchannel_del_interested_party(grpc_subchannel *c,
228 grpc_pollset *pollset) {
229 grpc_pollset_set_del_pollset(&c->pollset_set, pollset);
230}
231
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700232grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
233 grpc_subchannel_args *args) {
234 grpc_subchannel *c = gpr_malloc(sizeof(*c));
235 memset(c, 0, sizeof(*c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700236 c->refs = 1;
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700237 c->connector = connector;
238 grpc_connector_ref(c->connector);
Craig Tiller5945ee12015-06-27 10:36:09 -0700239 c->num_filters = args->filter_count;
240 c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700241 memcpy(c->filters, args->filters,
Craig Tiller5945ee12015-06-27 10:36:09 -0700242 sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700243 c->addr = gpr_malloc(args->addr_len);
244 memcpy(c->addr, args->addr, args->addr_len);
245 c->addr_len = args->addr_len;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700246 c->args = grpc_channel_args_copy(args->args);
Craig Tiller5f84c842015-06-26 16:08:21 -0700247 c->mdctx = args->mdctx;
Craig Tiller98465032015-06-29 14:36:42 -0700248 c->master = args->master;
Craig Tiller5f84c842015-06-26 16:08:21 -0700249 grpc_mdctx_ref(c->mdctx);
Craig Tillerff54c922015-06-26 16:57:20 -0700250 grpc_pollset_set_init(&c->pollset_set);
251 grpc_iomgr_closure_init(&c->connected, subchannel_connected, c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700252 grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700253 gpr_mu_init(&c->mu);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700254 return c;
255}
256
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700257static void start_connect(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700258 grpc_connect_in_args args;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700259
Craig Tiller4ab82d22015-06-29 09:40:33 -0700260 args.interested_parties = &c->pollset_set;
261 args.addr = c->addr;
262 args.addr_len = c->addr_len;
263 args.deadline = compute_connect_deadline(c);
264 args.channel_args = c->args;
265 args.metadata_context = c->mdctx;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700266
Craig Tiller4ab82d22015-06-29 09:40:33 -0700267 grpc_connector_connect(c->connector, &args, &c->connecting_result,
268 &c->connected);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700269}
270
Craig Tillerdf91ba52015-06-29 10:55:46 -0700271static void continue_creating_call(void *arg, int iomgr_success) {
272 waiting_for_connect *w4c = arg;
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700273 grpc_subchannel_create_call(w4c->subchannel, &w4c->initial_op, w4c->target,
274 w4c->notify);
Craig Tillerc3967532015-06-29 14:59:38 -0700275 GRPC_SUBCHANNEL_UNREF(w4c->subchannel, "waiting_for_connect");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700276 gpr_free(w4c);
277}
278
Craig Tillereb3b12e2015-06-26 14:42:49 -0700279void grpc_subchannel_create_call(grpc_subchannel *c,
Craig Tillereb3b12e2015-06-26 14:42:49 -0700280 grpc_transport_stream_op *initial_op,
281 grpc_subchannel_call **target,
282 grpc_iomgr_closure *notify) {
283 connection *con;
284 gpr_mu_lock(&c->mu);
285 if (c->active != NULL) {
286 con = c->active;
Craig Tillerc3967532015-06-29 14:59:38 -0700287 CONNECTION_REF_LOCKED(con, "call");
Craig Tillereb3b12e2015-06-26 14:42:49 -0700288 gpr_mu_unlock(&c->mu);
289
290 *target = create_call(con, initial_op);
291 notify->cb(notify->cb_arg, 1);
292 } else {
Craig Tiller5f84c842015-06-26 16:08:21 -0700293 waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c));
294 w4c->next = c->waiting;
295 w4c->notify = notify;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700296 w4c->initial_op = *initial_op;
Craig Tiller5f84c842015-06-26 16:08:21 -0700297 w4c->target = target;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700298 w4c->subchannel = c;
Craig Tiller98465032015-06-29 14:36:42 -0700299 /* released when clearing w4c */
Craig Tillerc3967532015-06-29 14:59:38 -0700300 SUBCHANNEL_REF_LOCKED(c, "waiting_for_connect");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700301 grpc_iomgr_closure_init(&w4c->continuation, continue_creating_call, w4c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700302 c->waiting = w4c;
303 grpc_subchannel_add_interested_party(c, initial_op->bind_pollset);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700304 if (!c->connecting) {
305 c->connecting = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700306 connectivity_state_changed_locked(c);
Craig Tiller98465032015-06-29 14:36:42 -0700307 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700308 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tillereb3b12e2015-06-26 14:42:49 -0700309 gpr_mu_unlock(&c->mu);
310
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700311 start_connect(c);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700312 } else {
313 gpr_mu_unlock(&c->mu);
314 }
315 }
316}
317
Craig Tiller5f84c842015-06-26 16:08:21 -0700318grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) {
319 grpc_connectivity_state state;
320 gpr_mu_lock(&c->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700321 state = grpc_connectivity_state_check(&c->state_tracker);
Craig Tiller5f84c842015-06-26 16:08:21 -0700322 gpr_mu_unlock(&c->mu);
323 return state;
324}
325
326void grpc_subchannel_notify_on_state_change(grpc_subchannel *c,
327 grpc_connectivity_state *state,
328 grpc_iomgr_closure *notify) {
Craig Tiller5f84c842015-06-26 16:08:21 -0700329 int do_connect = 0;
Craig Tiller5f84c842015-06-26 16:08:21 -0700330 gpr_mu_lock(&c->mu);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700331 if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state,
332 notify)) {
333 do_connect = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700334 c->connecting = 1;
Craig Tiller98465032015-06-29 14:36:42 -0700335 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700336 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tiller4ab82d22015-06-29 09:40:33 -0700337 grpc_connectivity_state_set(&c->state_tracker,
338 compute_connectivity_locked(c));
Craig Tiller5f84c842015-06-26 16:08:21 -0700339 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700340 gpr_mu_unlock(&c->mu);
Craig Tiller5f84c842015-06-26 16:08:21 -0700341 if (do_connect) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700342 start_connect(c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700343 }
344}
345
Craig Tiller4ab82d22015-06-29 09:40:33 -0700346void grpc_subchannel_process_transport_op(grpc_subchannel *c,
347 grpc_transport_op *op) {
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700348 connection *con = NULL;
349 grpc_subchannel *destroy;
350 gpr_mu_lock(&c->mu);
351 if (op->disconnect) {
352 c->disconnected = 1;
353 grpc_connectivity_state_set(&c->state_tracker,
354 compute_connectivity_locked(c));
355 }
356 if (c->active != NULL) {
357 con = c->active;
358 CONNECTION_REF_LOCKED(con, "transport-op");
359 }
360 gpr_mu_unlock(&c->mu);
361
362 if (con != NULL) {
363 grpc_channel_stack *channel_stack = CHANNEL_STACK_FROM_CONNECTION(con);
364 grpc_channel_element *top_elem = grpc_channel_stack_element(channel_stack, 0);
365 top_elem->filter->start_transport_op(top_elem, op);
366
367 gpr_mu_lock(&c->mu);
368 destroy = CONNECTION_UNREF_LOCKED(con, "transport-op");
369 gpr_mu_unlock(&c->mu);
370 if (destroy) {
371 subchannel_destroy(destroy);
372 }
373 }
Craig Tillerdf91ba52015-06-29 10:55:46 -0700374}
375
376static void on_state_changed(void *p, int iomgr_success) {
377 state_watcher *sw = p;
378 grpc_subchannel *c = sw->subchannel;
379 gpr_mu *mu = &c->mu;
380 int destroy;
381 grpc_transport_op op;
382 grpc_channel_element *elem;
383 connection *destroy_connection = NULL;
384 int do_connect = 0;
385
386 gpr_mu_lock(mu);
387
388 /* if we failed or there is a version number mismatch, just leave
389 this closure */
390 if (!iomgr_success || sw->subchannel->active_version != sw->version) {
391 goto done;
392 }
393
394 switch (sw->connectivity_state) {
395 case GRPC_CHANNEL_CONNECTING:
396 case GRPC_CHANNEL_READY:
397 case GRPC_CHANNEL_IDLE:
398 /* all is still good: keep watching */
399 memset(&op, 0, sizeof(op));
400 op.connectivity_state = &sw->connectivity_state;
401 op.on_connectivity_state_change = &sw->closure;
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700402 elem = grpc_channel_stack_element(
403 CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700404 elem->filter->start_transport_op(elem, &op);
405 /* early out */
406 gpr_mu_unlock(mu);
407 return;
408 case GRPC_CHANNEL_FATAL_FAILURE:
409 /* things have gone wrong, deactivate and enter idle */
410 if (sw->subchannel->active->refs == 0) {
411 destroy_connection = sw->subchannel->active;
412 }
413 sw->subchannel->active = NULL;
414 break;
415 case GRPC_CHANNEL_TRANSIENT_FAILURE:
416 /* things are starting to go wrong, reconnect but don't deactivate */
Craig Tiller98465032015-06-29 14:36:42 -0700417 /* released by connection */
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700418 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700419 do_connect = 1;
420 c->connecting = 1;
421 break;
422 }
423
424done:
425 grpc_connectivity_state_set(&c->state_tracker,
426 compute_connectivity_locked(c));
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700427 destroy = SUBCHANNEL_UNREF_LOCKED(c, "state_watcher");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700428 gpr_free(sw);
429 gpr_mu_unlock(mu);
430 if (do_connect) {
431 start_connect(c);
432 }
433 if (destroy) {
434 subchannel_destroy(c);
435 }
436 if (destroy_connection != NULL) {
437 connection_destroy(destroy_connection);
438 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700439}
440
Craig Tillerff54c922015-06-26 16:57:20 -0700441static void publish_transport(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700442 size_t channel_stack_size;
443 connection *con;
444 grpc_channel_stack *stk;
445 size_t num_filters;
446 const grpc_channel_filter **filters;
447 waiting_for_connect *w4c;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700448 grpc_transport_op op;
449 state_watcher *sw;
450 connection *destroy_connection = NULL;
451 grpc_channel_element *elem;
Craig Tiller5945ee12015-06-27 10:36:09 -0700452
Craig Tillerdf91ba52015-06-29 10:55:46 -0700453 /* build final filter list */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700454 num_filters = c->num_filters + c->connecting_result.num_filters + 1;
455 filters = gpr_malloc(sizeof(*filters) * num_filters);
456 memcpy(filters, c->filters, sizeof(*filters) * c->num_filters);
457 memcpy(filters + c->num_filters, c->connecting_result.filters,
458 sizeof(*filters) * c->connecting_result.num_filters);
459 filters[num_filters - 1] = &grpc_connected_channel_filter;
Craig Tiller5945ee12015-06-27 10:36:09 -0700460
Craig Tillerdf91ba52015-06-29 10:55:46 -0700461 /* construct channel stack */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700462 channel_stack_size = grpc_channel_stack_size(filters, num_filters);
463 con = gpr_malloc(sizeof(connection) + channel_stack_size);
464 stk = (grpc_channel_stack *)(con + 1);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700465 con->refs = 0;
466 con->subchannel = c;
Craig Tiller98465032015-06-29 14:36:42 -0700467 grpc_channel_stack_init(filters, num_filters, c->master, c->args, c->mdctx, stk);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700468 grpc_connected_channel_bind_transport(stk, c->connecting_result.transport);
Craig Tiller98465032015-06-29 14:36:42 -0700469 gpr_free(c->connecting_result.filters);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700470 memset(&c->connecting_result, 0, sizeof(c->connecting_result));
Craig Tillerff54c922015-06-26 16:57:20 -0700471
Craig Tillerdf91ba52015-06-29 10:55:46 -0700472 /* initialize state watcher */
473 sw = gpr_malloc(sizeof(*sw));
474 grpc_iomgr_closure_init(&sw->closure, on_state_changed, sw);
475 sw->subchannel = c;
476 sw->connectivity_state = GRPC_CHANNEL_READY;
477
Craig Tiller4ab82d22015-06-29 09:40:33 -0700478 gpr_mu_lock(&c->mu);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700479
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700480 if (c->disconnected) {
481 gpr_mu_unlock(&c->mu);
482 gpr_free(sw);
483 gpr_free(filters);
484 grpc_channel_stack_destroy(stk);
485 return;
486 }
487
Craig Tillerdf91ba52015-06-29 10:55:46 -0700488 /* publish */
489 if (c->active != NULL && c->active->refs == 0) {
490 destroy_connection = c->active;
491 }
Craig Tiller4ab82d22015-06-29 09:40:33 -0700492 c->active = con;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700493 c->active_version++;
494 sw->version = c->active_version;
Craig Tiller4ab82d22015-06-29 09:40:33 -0700495 c->connecting = 0;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700496
497 /* watch for changes; subchannel ref for connecting is donated
498 to the state watcher */
499 memset(&op, 0, sizeof(op));
500 op.connectivity_state = &sw->connectivity_state;
501 op.on_connectivity_state_change = &sw->closure;
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700502 SUBCHANNEL_REF_LOCKED(c, "state_watcher");
503 GPR_ASSERT(!SUBCHANNEL_UNREF_LOCKED(c, "connecting"));
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700504 elem =
505 grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700506 elem->filter->start_transport_op(elem, &op);
507
508 /* signal completion */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700509 connectivity_state_changed_locked(c);
510 while ((w4c = c->waiting)) {
Craig Tillerdf91ba52015-06-29 10:55:46 -0700511 c->waiting = w4c->next;
512 grpc_iomgr_add_callback(&w4c->continuation);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700513 }
Craig Tillerdf91ba52015-06-29 10:55:46 -0700514
Craig Tiller4ab82d22015-06-29 09:40:33 -0700515 gpr_mu_unlock(&c->mu);
Craig Tiller5945ee12015-06-27 10:36:09 -0700516
Craig Tiller4ab82d22015-06-29 09:40:33 -0700517 gpr_free(filters);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700518
Craig Tillerdf91ba52015-06-29 10:55:46 -0700519 if (destroy_connection != NULL) {
520 connection_destroy(destroy_connection);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700521 }
522}
Craig Tillerff54c922015-06-26 16:57:20 -0700523
524static void subchannel_connected(void *arg, int iomgr_success) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700525 grpc_subchannel *c = arg;
526 if (c->connecting_result.transport) {
527 publish_transport(c);
528 } else {
529 int destroy;
530 gpr_mu_lock(&c->mu);
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700531 destroy = SUBCHANNEL_UNREF_LOCKED(c, "connecting");
Craig Tiller4ab82d22015-06-29 09:40:33 -0700532 gpr_mu_unlock(&c->mu);
533 if (destroy) subchannel_destroy(c);
534 /* TODO(ctiller): retry after sleeping */
535 abort();
536 }
Craig Tillerff54c922015-06-26 16:57:20 -0700537}
538
Craig Tiller5f84c842015-06-26 16:08:21 -0700539static gpr_timespec compute_connect_deadline(grpc_subchannel *c) {
540 return gpr_time_add(gpr_now(), gpr_time_from_seconds(60));
541}
542
543static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) {
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700544 if (c->disconnected) {
545 return GRPC_CHANNEL_FATAL_FAILURE;
546 }
Craig Tiller5f84c842015-06-26 16:08:21 -0700547 if (c->connecting) {
548 return GRPC_CHANNEL_CONNECTING;
549 }
550 if (c->active) {
551 return GRPC_CHANNEL_READY;
552 }
553 return GRPC_CHANNEL_IDLE;
554}
555
556static void connectivity_state_changed_locked(grpc_subchannel *c) {
557 grpc_connectivity_state current = compute_connectivity_locked(c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700558 grpc_connectivity_state_set(&c->state_tracker, current);
Craig Tiller5f84c842015-06-26 16:08:21 -0700559}
560
Craig Tiller2595ab72015-06-25 15:26:00 -0700561/*
562 * grpc_subchannel_call implementation
563 */
564
Craig Tillerc3967532015-06-29 14:59:38 -0700565void grpc_subchannel_call_ref(grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
566 gpr_ref(&c->refs);
567}
Craig Tiller2595ab72015-06-25 15:26:00 -0700568
Craig Tillerc3967532015-06-29 14:59:38 -0700569void grpc_subchannel_call_unref(grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700570 if (gpr_unref(&c->refs)) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700571 gpr_mu *mu = &c->connection->subchannel->mu;
572 grpc_subchannel *destroy;
Craig Tillerca3e9d32015-06-27 18:37:27 -0700573 grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700574 gpr_mu_lock(mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700575 destroy = CONNECTION_UNREF_LOCKED(c->connection, "call");
Craig Tillerd7b68e72015-06-28 11:41:09 -0700576 gpr_mu_unlock(mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700577 gpr_free(c);
Craig Tillerc3967532015-06-29 14:59:38 -0700578 if (destroy != NULL) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700579 subchannel_destroy(destroy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700580 }
Craig Tiller2595ab72015-06-25 15:26:00 -0700581 }
582}
583
584void grpc_subchannel_call_process_op(grpc_subchannel_call *call,
585 grpc_transport_stream_op *op) {
586 grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
587 grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
588 top_elem->filter->start_transport_stream_op(top_elem, op);
589}
Craig Tillereb3b12e2015-06-26 14:42:49 -0700590
Craig Tiller4ab82d22015-06-29 09:40:33 -0700591grpc_subchannel_call *create_call(connection *con,
592 grpc_transport_stream_op *initial_op) {
593 grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con);
594 grpc_subchannel_call *call =
595 gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700596 grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call);
597 call->connection = con;
598 gpr_ref_init(&call->refs, 1);
599 grpc_call_stack_init(chanstk, NULL, initial_op, callstk);
600 return call;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700601}