blob: 3d065761ab916a589854fc086efae2412a6de305 [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 Tillereb3b12e2015-06-26 14:42:49 -070083
84 /** set during connection */
Craig Tiller04c5d4b2015-06-26 17:21:41 -070085 grpc_connect_out_args connecting_result;
Craig Tillereb3b12e2015-06-26 14:42:49 -070086
87 /** callback for connection finishing */
88 grpc_iomgr_closure connected;
89
Craig Tiller5f84c842015-06-26 16:08:21 -070090 /** pollset_set tracking who's interested in a connection
91 being setup */
92 grpc_pollset_set pollset_set;
93
Craig Tillereb3b12e2015-06-26 14:42:49 -070094 /** mutex protecting remaining elements */
95 gpr_mu mu;
96
97 /** active connection */
98 connection *active;
Craig Tillerdf91ba52015-06-29 10:55:46 -070099 /** version number for the active connection */
100 size_t active_version;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700101 /** refcount */
102 int refs;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700103 /** are we connecting */
104 int connecting;
Craig Tiller5f84c842015-06-26 16:08:21 -0700105 /** things waiting for a connection */
106 waiting_for_connect *waiting;
Craig Tillerc7b5f762015-06-27 11:48:42 -0700107 /** connectivity state tracking */
108 grpc_connectivity_state_tracker state_tracker;
Craig Tiller2595ab72015-06-25 15:26:00 -0700109};
110
111struct grpc_subchannel_call {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700112 connection *connection;
Craig Tiller2595ab72015-06-25 15:26:00 -0700113 gpr_refcount refs;
114};
115
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700116#define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1))
117#define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1))
Craig Tiller2595ab72015-06-25 15:26:00 -0700118
Craig Tiller4ab82d22015-06-29 09:40:33 -0700119static grpc_subchannel_call *create_call(connection *con,
120 grpc_transport_stream_op *initial_op);
Craig Tiller5f84c842015-06-26 16:08:21 -0700121static void connectivity_state_changed_locked(grpc_subchannel *c);
122static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c);
123static gpr_timespec compute_connect_deadline(grpc_subchannel *c);
Craig Tillerff54c922015-06-26 16:57:20 -0700124static void subchannel_connected(void *subchannel, int iomgr_success);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700125
Craig Tillerc3967532015-06-29 14:59:38 -0700126static void subchannel_ref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
127static int subchannel_unref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT;
128static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
129static grpc_subchannel *connection_unref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
Craig Tiller4ab82d22015-06-29 09:40:33 -0700130 GRPC_MUST_USE_RESULT;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700131static void subchannel_destroy(grpc_subchannel *c);
132
Craig Tillerc3967532015-06-29 14:59:38 -0700133#ifdef GRPC_SUBCHANNEL_REFCOUNT_DEBUG
134#define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p), __FILE__, __LINE__, (r))
135#define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p), __FILE__, __LINE__, (r))
136#define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p), __FILE__, __LINE__, (r))
137#define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p), __FILE__, __LINE__, (r))
138#define REF_PASS_ARGS , file, line, reason
139#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)
140#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)
141#else
142#define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p))
143#define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p))
144#define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p), __FILE__, __LINE__, (r))
145#define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p), __FILE__, __LINE__, (r))
146#define REF_PASS_ARGS
147#endif
148
Craig Tiller2595ab72015-06-25 15:26:00 -0700149/*
Craig Tillerca3e9d32015-06-27 18:37:27 -0700150 * connection implementation
151 */
152
Craig Tillerd7b68e72015-06-28 11:41:09 -0700153static void connection_destroy(connection *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700154 GPR_ASSERT(c->refs == 0);
155 grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CONNECTION(c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700156 gpr_free(c);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700157}
158
Craig Tillerc3967532015-06-29 14:59:38 -0700159static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
160 REF_LOG("CONNECTION", c);
161 subchannel_ref_locked(c->subchannel REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700162 ++c->refs;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700163}
164
Craig Tillerc3967532015-06-29 14:59:38 -0700165static grpc_subchannel *connection_unref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700166 grpc_subchannel *destroy = NULL;
Craig Tillerc3967532015-06-29 14:59:38 -0700167 UNREF_LOG("CONNECTION", c);
168 if (subchannel_unref_locked(c->subchannel REF_PASS_ARGS)) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700169 destroy = c->subchannel;
170 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700171 if (--c->refs == 0 && c->subchannel->active != c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700172 connection_destroy(c);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700173 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700174 return destroy;
Craig Tillerca3e9d32015-06-27 18:37:27 -0700175}
176
177/*
Craig Tiller2595ab72015-06-25 15:26:00 -0700178 * grpc_subchannel implementation
179 */
180
Craig Tillerc3967532015-06-29 14:59:38 -0700181static void subchannel_ref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
182 REF_LOG("SUBCHANNEL", c);
183 ++c->refs;
184}
Craig Tiller2595ab72015-06-25 15:26:00 -0700185
Craig Tillerc3967532015-06-29 14:59:38 -0700186static int subchannel_unref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
187 UNREF_LOG("SUBCHANNEL", c);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700188 return --c->refs == 0;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700189}
190
Craig Tillerc3967532015-06-29 14:59:38 -0700191void grpc_subchannel_ref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700192 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700193 subchannel_ref_locked(c REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700194 gpr_mu_unlock(&c->mu);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700195}
196
Craig Tillerc3967532015-06-29 14:59:38 -0700197void grpc_subchannel_unref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700198 int destroy;
199 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700200 destroy = subchannel_unref_locked(c REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700201 gpr_mu_unlock(&c->mu);
202 if (destroy) subchannel_destroy(c);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700203}
204
205static void subchannel_destroy(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700206 if (c->active != NULL) {
207 connection_destroy(c->active);
208 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700209 gpr_free(c->filters);
210 grpc_channel_args_destroy(c->args);
211 gpr_free(c->addr);
212 grpc_mdctx_unref(c->mdctx);
213 grpc_pollset_set_destroy(&c->pollset_set);
214 grpc_connectivity_state_destroy(&c->state_tracker);
Craig Tillerc3967532015-06-29 14:59:38 -0700215 grpc_connector_unref(c->connector);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700216 gpr_free(c);
Craig Tiller2595ab72015-06-25 15:26:00 -0700217}
218
Craig Tiller5f84c842015-06-26 16:08:21 -0700219void grpc_subchannel_add_interested_party(grpc_subchannel *c,
220 grpc_pollset *pollset) {
221 grpc_pollset_set_add_pollset(&c->pollset_set, pollset);
222}
223
224void grpc_subchannel_del_interested_party(grpc_subchannel *c,
225 grpc_pollset *pollset) {
226 grpc_pollset_set_del_pollset(&c->pollset_set, pollset);
227}
228
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700229grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
230 grpc_subchannel_args *args) {
231 grpc_subchannel *c = gpr_malloc(sizeof(*c));
232 memset(c, 0, sizeof(*c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700233 c->refs = 1;
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700234 c->connector = connector;
235 grpc_connector_ref(c->connector);
Craig Tiller5945ee12015-06-27 10:36:09 -0700236 c->num_filters = args->filter_count;
237 c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700238 memcpy(c->filters, args->filters,
Craig Tiller5945ee12015-06-27 10:36:09 -0700239 sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700240 c->addr = gpr_malloc(args->addr_len);
241 memcpy(c->addr, args->addr, args->addr_len);
242 c->addr_len = args->addr_len;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700243 c->args = grpc_channel_args_copy(args->args);
Craig Tiller5f84c842015-06-26 16:08:21 -0700244 c->mdctx = args->mdctx;
Craig Tiller98465032015-06-29 14:36:42 -0700245 c->master = args->master;
Craig Tiller5f84c842015-06-26 16:08:21 -0700246 grpc_mdctx_ref(c->mdctx);
Craig Tillerff54c922015-06-26 16:57:20 -0700247 grpc_pollset_set_init(&c->pollset_set);
248 grpc_iomgr_closure_init(&c->connected, subchannel_connected, c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700249 grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700250 gpr_mu_init(&c->mu);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700251 return c;
252}
253
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700254static void start_connect(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700255 grpc_connect_in_args args;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700256
Craig Tiller4ab82d22015-06-29 09:40:33 -0700257 args.interested_parties = &c->pollset_set;
258 args.addr = c->addr;
259 args.addr_len = c->addr_len;
260 args.deadline = compute_connect_deadline(c);
261 args.channel_args = c->args;
262 args.metadata_context = c->mdctx;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700263
Craig Tiller4ab82d22015-06-29 09:40:33 -0700264 grpc_connector_connect(c->connector, &args, &c->connecting_result,
265 &c->connected);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700266}
267
Craig Tillerdf91ba52015-06-29 10:55:46 -0700268static void continue_creating_call(void *arg, int iomgr_success) {
269 waiting_for_connect *w4c = arg;
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700270 grpc_subchannel_create_call(w4c->subchannel, &w4c->initial_op, w4c->target,
271 w4c->notify);
Craig Tillerc3967532015-06-29 14:59:38 -0700272 GRPC_SUBCHANNEL_UNREF(w4c->subchannel, "waiting_for_connect");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700273 gpr_free(w4c);
274}
275
Craig Tillereb3b12e2015-06-26 14:42:49 -0700276void grpc_subchannel_create_call(grpc_subchannel *c,
Craig Tillereb3b12e2015-06-26 14:42:49 -0700277 grpc_transport_stream_op *initial_op,
278 grpc_subchannel_call **target,
279 grpc_iomgr_closure *notify) {
280 connection *con;
281 gpr_mu_lock(&c->mu);
282 if (c->active != NULL) {
283 con = c->active;
Craig Tillerc3967532015-06-29 14:59:38 -0700284 CONNECTION_REF_LOCKED(con, "call");
Craig Tillereb3b12e2015-06-26 14:42:49 -0700285 gpr_mu_unlock(&c->mu);
286
287 *target = create_call(con, initial_op);
288 notify->cb(notify->cb_arg, 1);
289 } else {
Craig Tiller5f84c842015-06-26 16:08:21 -0700290 waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c));
291 w4c->next = c->waiting;
292 w4c->notify = notify;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700293 w4c->initial_op = *initial_op;
Craig Tiller5f84c842015-06-26 16:08:21 -0700294 w4c->target = target;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700295 w4c->subchannel = c;
Craig Tiller98465032015-06-29 14:36:42 -0700296 /* released when clearing w4c */
Craig Tillerc3967532015-06-29 14:59:38 -0700297 SUBCHANNEL_REF_LOCKED(c, "waiting_for_connect");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700298 grpc_iomgr_closure_init(&w4c->continuation, continue_creating_call, w4c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700299 c->waiting = w4c;
300 grpc_subchannel_add_interested_party(c, initial_op->bind_pollset);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700301 if (!c->connecting) {
302 c->connecting = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700303 connectivity_state_changed_locked(c);
Craig Tiller98465032015-06-29 14:36:42 -0700304 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700305 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tillereb3b12e2015-06-26 14:42:49 -0700306 gpr_mu_unlock(&c->mu);
307
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700308 start_connect(c);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700309 } else {
310 gpr_mu_unlock(&c->mu);
311 }
312 }
313}
314
Craig Tiller5f84c842015-06-26 16:08:21 -0700315grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) {
316 grpc_connectivity_state state;
317 gpr_mu_lock(&c->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700318 state = grpc_connectivity_state_check(&c->state_tracker);
Craig Tiller5f84c842015-06-26 16:08:21 -0700319 gpr_mu_unlock(&c->mu);
320 return state;
321}
322
323void grpc_subchannel_notify_on_state_change(grpc_subchannel *c,
324 grpc_connectivity_state *state,
325 grpc_iomgr_closure *notify) {
Craig Tiller5f84c842015-06-26 16:08:21 -0700326 int do_connect = 0;
Craig Tiller5f84c842015-06-26 16:08:21 -0700327 gpr_mu_lock(&c->mu);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700328 if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state,
329 notify)) {
330 do_connect = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700331 c->connecting = 1;
Craig Tiller98465032015-06-29 14:36:42 -0700332 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700333 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tiller4ab82d22015-06-29 09:40:33 -0700334 grpc_connectivity_state_set(&c->state_tracker,
335 compute_connectivity_locked(c));
Craig Tiller5f84c842015-06-26 16:08:21 -0700336 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700337 gpr_mu_unlock(&c->mu);
Craig Tiller5f84c842015-06-26 16:08:21 -0700338 if (do_connect) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700339 start_connect(c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700340 }
341}
342
Craig Tiller4ab82d22015-06-29 09:40:33 -0700343void grpc_subchannel_process_transport_op(grpc_subchannel *c,
344 grpc_transport_op *op) {
Craig Tiller98465032015-06-29 14:36:42 -0700345 gpr_log(GPR_ERROR, "grpc_subchannel_process_transport_op not implemented");
346 abort();
Craig Tillerdf91ba52015-06-29 10:55:46 -0700347}
348
349static void on_state_changed(void *p, int iomgr_success) {
350 state_watcher *sw = p;
351 grpc_subchannel *c = sw->subchannel;
352 gpr_mu *mu = &c->mu;
353 int destroy;
354 grpc_transport_op op;
355 grpc_channel_element *elem;
356 connection *destroy_connection = NULL;
357 int do_connect = 0;
358
359 gpr_mu_lock(mu);
360
361 /* if we failed or there is a version number mismatch, just leave
362 this closure */
363 if (!iomgr_success || sw->subchannel->active_version != sw->version) {
364 goto done;
365 }
366
367 switch (sw->connectivity_state) {
368 case GRPC_CHANNEL_CONNECTING:
369 case GRPC_CHANNEL_READY:
370 case GRPC_CHANNEL_IDLE:
371 /* all is still good: keep watching */
372 memset(&op, 0, sizeof(op));
373 op.connectivity_state = &sw->connectivity_state;
374 op.on_connectivity_state_change = &sw->closure;
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700375 elem = grpc_channel_stack_element(
376 CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700377 elem->filter->start_transport_op(elem, &op);
378 /* early out */
379 gpr_mu_unlock(mu);
380 return;
381 case GRPC_CHANNEL_FATAL_FAILURE:
382 /* things have gone wrong, deactivate and enter idle */
383 if (sw->subchannel->active->refs == 0) {
384 destroy_connection = sw->subchannel->active;
385 }
386 sw->subchannel->active = NULL;
387 break;
388 case GRPC_CHANNEL_TRANSIENT_FAILURE:
389 /* things are starting to go wrong, reconnect but don't deactivate */
Craig Tiller98465032015-06-29 14:36:42 -0700390 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700391 SUBCHANNEL_REF_LOCKED(c, "connection");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700392 do_connect = 1;
393 c->connecting = 1;
394 break;
395 }
396
397done:
398 grpc_connectivity_state_set(&c->state_tracker,
399 compute_connectivity_locked(c));
Craig Tillerc3967532015-06-29 14:59:38 -0700400 destroy = SUBCHANNEL_UNREF_LOCKED(c, "connection");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700401 gpr_free(sw);
402 gpr_mu_unlock(mu);
403 if (do_connect) {
404 start_connect(c);
405 }
406 if (destroy) {
407 subchannel_destroy(c);
408 }
409 if (destroy_connection != NULL) {
410 connection_destroy(destroy_connection);
411 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700412}
413
Craig Tillerff54c922015-06-26 16:57:20 -0700414static void publish_transport(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700415 size_t channel_stack_size;
416 connection *con;
417 grpc_channel_stack *stk;
418 size_t num_filters;
419 const grpc_channel_filter **filters;
420 waiting_for_connect *w4c;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700421 grpc_transport_op op;
422 state_watcher *sw;
423 connection *destroy_connection = NULL;
424 grpc_channel_element *elem;
Craig Tiller5945ee12015-06-27 10:36:09 -0700425
Craig Tillerdf91ba52015-06-29 10:55:46 -0700426 /* build final filter list */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700427 num_filters = c->num_filters + c->connecting_result.num_filters + 1;
428 filters = gpr_malloc(sizeof(*filters) * num_filters);
429 memcpy(filters, c->filters, sizeof(*filters) * c->num_filters);
430 memcpy(filters + c->num_filters, c->connecting_result.filters,
431 sizeof(*filters) * c->connecting_result.num_filters);
432 filters[num_filters - 1] = &grpc_connected_channel_filter;
Craig Tiller5945ee12015-06-27 10:36:09 -0700433
Craig Tillerdf91ba52015-06-29 10:55:46 -0700434 /* construct channel stack */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700435 channel_stack_size = grpc_channel_stack_size(filters, num_filters);
436 con = gpr_malloc(sizeof(connection) + channel_stack_size);
437 stk = (grpc_channel_stack *)(con + 1);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700438 con->refs = 0;
439 con->subchannel = c;
Craig Tiller98465032015-06-29 14:36:42 -0700440 grpc_channel_stack_init(filters, num_filters, c->master, c->args, c->mdctx, stk);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700441 grpc_connected_channel_bind_transport(stk, c->connecting_result.transport);
Craig Tiller98465032015-06-29 14:36:42 -0700442 gpr_free(c->connecting_result.filters);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700443 memset(&c->connecting_result, 0, sizeof(c->connecting_result));
Craig Tillerff54c922015-06-26 16:57:20 -0700444
Craig Tillerdf91ba52015-06-29 10:55:46 -0700445 /* initialize state watcher */
446 sw = gpr_malloc(sizeof(*sw));
447 grpc_iomgr_closure_init(&sw->closure, on_state_changed, sw);
448 sw->subchannel = c;
449 sw->connectivity_state = GRPC_CHANNEL_READY;
450
Craig Tiller4ab82d22015-06-29 09:40:33 -0700451 gpr_mu_lock(&c->mu);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700452
453 /* publish */
454 if (c->active != NULL && c->active->refs == 0) {
455 destroy_connection = c->active;
456 }
Craig Tiller4ab82d22015-06-29 09:40:33 -0700457 c->active = con;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700458 c->active_version++;
459 sw->version = c->active_version;
Craig Tiller4ab82d22015-06-29 09:40:33 -0700460 c->connecting = 0;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700461
462 /* watch for changes; subchannel ref for connecting is donated
463 to the state watcher */
464 memset(&op, 0, sizeof(op));
465 op.connectivity_state = &sw->connectivity_state;
466 op.on_connectivity_state_change = &sw->closure;
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700467 elem =
468 grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700469 elem->filter->start_transport_op(elem, &op);
470
471 /* signal completion */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700472 connectivity_state_changed_locked(c);
473 while ((w4c = c->waiting)) {
Craig Tillerdf91ba52015-06-29 10:55:46 -0700474 c->waiting = w4c->next;
475 grpc_iomgr_add_callback(&w4c->continuation);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700476 }
Craig Tillerdf91ba52015-06-29 10:55:46 -0700477
Craig Tiller4ab82d22015-06-29 09:40:33 -0700478 gpr_mu_unlock(&c->mu);
Craig Tiller5945ee12015-06-27 10:36:09 -0700479
Craig Tiller4ab82d22015-06-29 09:40:33 -0700480 gpr_free(filters);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700481
Craig Tillerdf91ba52015-06-29 10:55:46 -0700482 if (destroy_connection != NULL) {
483 connection_destroy(destroy_connection);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700484 }
485}
Craig Tillerff54c922015-06-26 16:57:20 -0700486
487static void subchannel_connected(void *arg, int iomgr_success) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700488 grpc_subchannel *c = arg;
489 if (c->connecting_result.transport) {
490 publish_transport(c);
491 } else {
492 int destroy;
493 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700494 destroy = SUBCHANNEL_UNREF_LOCKED(c, "connection");
Craig Tiller4ab82d22015-06-29 09:40:33 -0700495 gpr_mu_unlock(&c->mu);
496 if (destroy) subchannel_destroy(c);
497 /* TODO(ctiller): retry after sleeping */
498 abort();
499 }
Craig Tillerff54c922015-06-26 16:57:20 -0700500}
501
Craig Tiller5f84c842015-06-26 16:08:21 -0700502static gpr_timespec compute_connect_deadline(grpc_subchannel *c) {
503 return gpr_time_add(gpr_now(), gpr_time_from_seconds(60));
504}
505
506static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) {
507 if (c->connecting) {
508 return GRPC_CHANNEL_CONNECTING;
509 }
510 if (c->active) {
511 return GRPC_CHANNEL_READY;
512 }
513 return GRPC_CHANNEL_IDLE;
514}
515
516static void connectivity_state_changed_locked(grpc_subchannel *c) {
517 grpc_connectivity_state current = compute_connectivity_locked(c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700518 grpc_connectivity_state_set(&c->state_tracker, current);
Craig Tiller5f84c842015-06-26 16:08:21 -0700519}
520
Craig Tiller2595ab72015-06-25 15:26:00 -0700521/*
522 * grpc_subchannel_call implementation
523 */
524
Craig Tillerc3967532015-06-29 14:59:38 -0700525void grpc_subchannel_call_ref(grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
526 gpr_ref(&c->refs);
527}
Craig Tiller2595ab72015-06-25 15:26:00 -0700528
Craig Tillerc3967532015-06-29 14:59:38 -0700529void grpc_subchannel_call_unref(grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700530 if (gpr_unref(&c->refs)) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700531 gpr_mu *mu = &c->connection->subchannel->mu;
532 grpc_subchannel *destroy;
Craig Tillerca3e9d32015-06-27 18:37:27 -0700533 grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700534 gpr_mu_lock(mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700535 destroy = CONNECTION_UNREF_LOCKED(c->connection, "call");
Craig Tillerd7b68e72015-06-28 11:41:09 -0700536 gpr_mu_unlock(mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700537 gpr_free(c);
Craig Tillerc3967532015-06-29 14:59:38 -0700538 if (destroy != NULL) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700539 subchannel_destroy(destroy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700540 }
Craig Tiller2595ab72015-06-25 15:26:00 -0700541 }
542}
543
544void grpc_subchannel_call_process_op(grpc_subchannel_call *call,
545 grpc_transport_stream_op *op) {
546 grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
547 grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
548 top_elem->filter->start_transport_stream_op(top_elem, op);
549}
Craig Tillereb3b12e2015-06-26 14:42:49 -0700550
Craig Tiller4ab82d22015-06-29 09:40:33 -0700551grpc_subchannel_call *create_call(connection *con,
552 grpc_transport_stream_op *initial_op) {
553 grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con);
554 grpc_subchannel_call *call =
555 gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700556 grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call);
557 call->connection = con;
558 gpr_ref_init(&call->refs, 1);
559 grpc_call_stack_init(chanstk, NULL, initial_op, callstk);
560 return call;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700561}