blob: b5e991a594474c81a61c46ae7fc6b18ba6fd4d45 [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 Tillerabf36382015-06-29 16:13:27 -070062 grpc_pollset *pollset;
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 Tillerabf36382015-06-29 16:13:27 -0700121static grpc_subchannel_call *create_call(connection *con);
Craig Tiller5f84c842015-06-26 16:08:21 -0700122static void connectivity_state_changed_locked(grpc_subchannel *c);
123static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c);
124static gpr_timespec compute_connect_deadline(grpc_subchannel *c);
Craig Tillerff54c922015-06-26 16:57:20 -0700125static void subchannel_connected(void *subchannel, int iomgr_success);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700126
Craig Tillerc3967532015-06-29 14:59:38 -0700127static void subchannel_ref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
128static int subchannel_unref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT;
129static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
130static grpc_subchannel *connection_unref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
Craig Tiller4ab82d22015-06-29 09:40:33 -0700131 GRPC_MUST_USE_RESULT;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700132static void subchannel_destroy(grpc_subchannel *c);
133
Craig Tillerc3967532015-06-29 14:59:38 -0700134#ifdef GRPC_SUBCHANNEL_REFCOUNT_DEBUG
135#define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p), __FILE__, __LINE__, (r))
136#define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p), __FILE__, __LINE__, (r))
137#define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p), __FILE__, __LINE__, (r))
138#define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p), __FILE__, __LINE__, (r))
139#define REF_PASS_ARGS , file, line, reason
140#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)
141#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)
142#else
143#define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p))
144#define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p))
145#define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p), __FILE__, __LINE__, (r))
146#define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p), __FILE__, __LINE__, (r))
147#define REF_PASS_ARGS
148#endif
149
Craig Tiller2595ab72015-06-25 15:26:00 -0700150/*
Craig Tillerca3e9d32015-06-27 18:37:27 -0700151 * connection implementation
152 */
153
Craig Tillerd7b68e72015-06-28 11:41:09 -0700154static void connection_destroy(connection *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700155 GPR_ASSERT(c->refs == 0);
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700156 gpr_log(GPR_DEBUG, "CONNECTION_DESTROY %p", c);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700157 grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CONNECTION(c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700158 gpr_free(c);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700159}
160
Craig Tillerc3967532015-06-29 14:59:38 -0700161static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
162 REF_LOG("CONNECTION", c);
163 subchannel_ref_locked(c->subchannel REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700164 ++c->refs;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700165}
166
Craig Tillerc3967532015-06-29 14:59:38 -0700167static grpc_subchannel *connection_unref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700168 grpc_subchannel *destroy = NULL;
Craig Tillerc3967532015-06-29 14:59:38 -0700169 UNREF_LOG("CONNECTION", c);
170 if (subchannel_unref_locked(c->subchannel REF_PASS_ARGS)) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700171 destroy = c->subchannel;
172 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700173 if (--c->refs == 0 && c->subchannel->active != c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700174 connection_destroy(c);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700175 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700176 return destroy;
Craig Tillerca3e9d32015-06-27 18:37:27 -0700177}
178
179/*
Craig Tiller2595ab72015-06-25 15:26:00 -0700180 * grpc_subchannel implementation
181 */
182
Craig Tillerc3967532015-06-29 14:59:38 -0700183static void subchannel_ref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
184 REF_LOG("SUBCHANNEL", c);
185 ++c->refs;
186}
Craig Tiller2595ab72015-06-25 15:26:00 -0700187
Craig Tillerc3967532015-06-29 14:59:38 -0700188static int subchannel_unref_locked(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
189 UNREF_LOG("SUBCHANNEL", c);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700190 return --c->refs == 0;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700191}
192
Craig Tillerc3967532015-06-29 14:59:38 -0700193void grpc_subchannel_ref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700194 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700195 subchannel_ref_locked(c REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700196 gpr_mu_unlock(&c->mu);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700197}
198
Craig Tillerc3967532015-06-29 14:59:38 -0700199void grpc_subchannel_unref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700200 int destroy;
201 gpr_mu_lock(&c->mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700202 destroy = subchannel_unref_locked(c REF_PASS_ARGS);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700203 gpr_mu_unlock(&c->mu);
204 if (destroy) subchannel_destroy(c);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700205}
206
207static void subchannel_destroy(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700208 if (c->active != NULL) {
209 connection_destroy(c->active);
210 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700211 gpr_free(c->filters);
212 grpc_channel_args_destroy(c->args);
213 gpr_free(c->addr);
214 grpc_mdctx_unref(c->mdctx);
215 grpc_pollset_set_destroy(&c->pollset_set);
216 grpc_connectivity_state_destroy(&c->state_tracker);
Craig Tillerc3967532015-06-29 14:59:38 -0700217 grpc_connector_unref(c->connector);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700218 gpr_free(c);
Craig Tiller2595ab72015-06-25 15:26:00 -0700219}
220
Craig Tiller5f84c842015-06-26 16:08:21 -0700221void grpc_subchannel_add_interested_party(grpc_subchannel *c,
222 grpc_pollset *pollset) {
223 grpc_pollset_set_add_pollset(&c->pollset_set, pollset);
224}
225
226void grpc_subchannel_del_interested_party(grpc_subchannel *c,
227 grpc_pollset *pollset) {
228 grpc_pollset_set_del_pollset(&c->pollset_set, pollset);
229}
230
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700231grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
232 grpc_subchannel_args *args) {
233 grpc_subchannel *c = gpr_malloc(sizeof(*c));
234 memset(c, 0, sizeof(*c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700235 c->refs = 1;
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700236 c->connector = connector;
237 grpc_connector_ref(c->connector);
Craig Tiller5945ee12015-06-27 10:36:09 -0700238 c->num_filters = args->filter_count;
239 c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700240 memcpy(c->filters, args->filters,
Craig Tiller5945ee12015-06-27 10:36:09 -0700241 sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700242 c->addr = gpr_malloc(args->addr_len);
243 memcpy(c->addr, args->addr, args->addr_len);
244 c->addr_len = args->addr_len;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700245 c->args = grpc_channel_args_copy(args->args);
Craig Tiller5f84c842015-06-26 16:08:21 -0700246 c->mdctx = args->mdctx;
Craig Tiller98465032015-06-29 14:36:42 -0700247 c->master = args->master;
Craig Tiller5f84c842015-06-26 16:08:21 -0700248 grpc_mdctx_ref(c->mdctx);
Craig Tillerff54c922015-06-26 16:57:20 -0700249 grpc_pollset_set_init(&c->pollset_set);
250 grpc_iomgr_closure_init(&c->connected, subchannel_connected, c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700251 grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700252 gpr_mu_init(&c->mu);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700253 return c;
254}
255
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700256static void start_connect(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700257 grpc_connect_in_args args;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700258
Craig Tiller4ab82d22015-06-29 09:40:33 -0700259 args.interested_parties = &c->pollset_set;
260 args.addr = c->addr;
261 args.addr_len = c->addr_len;
262 args.deadline = compute_connect_deadline(c);
263 args.channel_args = c->args;
264 args.metadata_context = c->mdctx;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700265
Craig Tiller4ab82d22015-06-29 09:40:33 -0700266 grpc_connector_connect(c->connector, &args, &c->connecting_result,
267 &c->connected);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700268}
269
Craig Tillerdf91ba52015-06-29 10:55:46 -0700270static void continue_creating_call(void *arg, int iomgr_success) {
271 waiting_for_connect *w4c = arg;
Craig Tillerabf36382015-06-29 16:13:27 -0700272 grpc_subchannel_create_call(w4c->subchannel, w4c->pollset, w4c->target,
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700273 w4c->notify);
Craig Tillerc3967532015-06-29 14:59:38 -0700274 GRPC_SUBCHANNEL_UNREF(w4c->subchannel, "waiting_for_connect");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700275 gpr_free(w4c);
276}
277
Craig Tillerabf36382015-06-29 16:13:27 -0700278void grpc_subchannel_create_call(grpc_subchannel *c, grpc_pollset *pollset,
Craig Tillereb3b12e2015-06-26 14:42:49 -0700279 grpc_subchannel_call **target,
280 grpc_iomgr_closure *notify) {
281 connection *con;
282 gpr_mu_lock(&c->mu);
283 if (c->active != NULL) {
284 con = c->active;
Craig Tillerc3967532015-06-29 14:59:38 -0700285 CONNECTION_REF_LOCKED(con, "call");
Craig Tillereb3b12e2015-06-26 14:42:49 -0700286 gpr_mu_unlock(&c->mu);
287
Craig Tillerabf36382015-06-29 16:13:27 -0700288 *target = create_call(con);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700289 notify->cb(notify->cb_arg, 1);
290 } else {
Craig Tiller5f84c842015-06-26 16:08:21 -0700291 waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c));
292 w4c->next = c->waiting;
293 w4c->notify = notify;
Craig Tillerabf36382015-06-29 16:13:27 -0700294 w4c->pollset = pollset;
Craig Tiller5f84c842015-06-26 16:08:21 -0700295 w4c->target = target;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700296 w4c->subchannel = c;
Craig Tiller98465032015-06-29 14:36:42 -0700297 /* released when clearing w4c */
Craig Tillerc3967532015-06-29 14:59:38 -0700298 SUBCHANNEL_REF_LOCKED(c, "waiting_for_connect");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700299 grpc_iomgr_closure_init(&w4c->continuation, continue_creating_call, w4c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700300 c->waiting = w4c;
Craig Tillerabf36382015-06-29 16:13:27 -0700301 grpc_subchannel_add_interested_party(c, pollset);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700302 if (!c->connecting) {
303 c->connecting = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700304 connectivity_state_changed_locked(c);
Craig Tiller98465032015-06-29 14:36:42 -0700305 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700306 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tillereb3b12e2015-06-26 14:42:49 -0700307 gpr_mu_unlock(&c->mu);
308
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700309 start_connect(c);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700310 } else {
311 gpr_mu_unlock(&c->mu);
312 }
313 }
314}
315
Craig Tiller5f84c842015-06-26 16:08:21 -0700316grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) {
317 grpc_connectivity_state state;
318 gpr_mu_lock(&c->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700319 state = grpc_connectivity_state_check(&c->state_tracker);
Craig Tiller5f84c842015-06-26 16:08:21 -0700320 gpr_mu_unlock(&c->mu);
321 return state;
322}
323
324void grpc_subchannel_notify_on_state_change(grpc_subchannel *c,
325 grpc_connectivity_state *state,
326 grpc_iomgr_closure *notify) {
Craig Tiller5f84c842015-06-26 16:08:21 -0700327 int do_connect = 0;
Craig Tiller5f84c842015-06-26 16:08:21 -0700328 gpr_mu_lock(&c->mu);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700329 if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state,
330 notify)) {
331 do_connect = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700332 c->connecting = 1;
Craig Tiller98465032015-06-29 14:36:42 -0700333 /* released by connection */
Craig Tillerc3967532015-06-29 14:59:38 -0700334 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tiller4ab82d22015-06-29 09:40:33 -0700335 grpc_connectivity_state_set(&c->state_tracker,
336 compute_connectivity_locked(c));
Craig Tiller5f84c842015-06-26 16:08:21 -0700337 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700338 gpr_mu_unlock(&c->mu);
Craig Tiller5f84c842015-06-26 16:08:21 -0700339 if (do_connect) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700340 start_connect(c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700341 }
342}
343
Craig Tiller4ab82d22015-06-29 09:40:33 -0700344void grpc_subchannel_process_transport_op(grpc_subchannel *c,
345 grpc_transport_op *op) {
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700346 connection *con = NULL;
347 grpc_subchannel *destroy;
348 gpr_mu_lock(&c->mu);
349 if (op->disconnect) {
350 c->disconnected = 1;
351 grpc_connectivity_state_set(&c->state_tracker,
352 compute_connectivity_locked(c));
353 }
354 if (c->active != NULL) {
355 con = c->active;
356 CONNECTION_REF_LOCKED(con, "transport-op");
357 }
358 gpr_mu_unlock(&c->mu);
359
360 if (con != NULL) {
361 grpc_channel_stack *channel_stack = CHANNEL_STACK_FROM_CONNECTION(con);
362 grpc_channel_element *top_elem = grpc_channel_stack_element(channel_stack, 0);
363 top_elem->filter->start_transport_op(top_elem, op);
364
365 gpr_mu_lock(&c->mu);
366 destroy = CONNECTION_UNREF_LOCKED(con, "transport-op");
367 gpr_mu_unlock(&c->mu);
368 if (destroy) {
369 subchannel_destroy(destroy);
370 }
371 }
Craig Tillerdf91ba52015-06-29 10:55:46 -0700372}
373
374static void on_state_changed(void *p, int iomgr_success) {
375 state_watcher *sw = p;
376 grpc_subchannel *c = sw->subchannel;
377 gpr_mu *mu = &c->mu;
378 int destroy;
379 grpc_transport_op op;
380 grpc_channel_element *elem;
381 connection *destroy_connection = NULL;
382 int do_connect = 0;
383
384 gpr_mu_lock(mu);
385
386 /* if we failed or there is a version number mismatch, just leave
387 this closure */
388 if (!iomgr_success || sw->subchannel->active_version != sw->version) {
389 goto done;
390 }
391
392 switch (sw->connectivity_state) {
393 case GRPC_CHANNEL_CONNECTING:
394 case GRPC_CHANNEL_READY:
395 case GRPC_CHANNEL_IDLE:
396 /* all is still good: keep watching */
397 memset(&op, 0, sizeof(op));
398 op.connectivity_state = &sw->connectivity_state;
399 op.on_connectivity_state_change = &sw->closure;
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700400 elem = grpc_channel_stack_element(
401 CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700402 elem->filter->start_transport_op(elem, &op);
403 /* early out */
404 gpr_mu_unlock(mu);
405 return;
406 case GRPC_CHANNEL_FATAL_FAILURE:
407 /* things have gone wrong, deactivate and enter idle */
408 if (sw->subchannel->active->refs == 0) {
409 destroy_connection = sw->subchannel->active;
410 }
411 sw->subchannel->active = NULL;
412 break;
413 case GRPC_CHANNEL_TRANSIENT_FAILURE:
414 /* things are starting to go wrong, reconnect but don't deactivate */
Craig Tiller98465032015-06-29 14:36:42 -0700415 /* released by connection */
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700416 SUBCHANNEL_REF_LOCKED(c, "connecting");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700417 do_connect = 1;
418 c->connecting = 1;
419 break;
420 }
421
422done:
423 grpc_connectivity_state_set(&c->state_tracker,
424 compute_connectivity_locked(c));
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700425 destroy = SUBCHANNEL_UNREF_LOCKED(c, "state_watcher");
Craig Tillerdf91ba52015-06-29 10:55:46 -0700426 gpr_free(sw);
427 gpr_mu_unlock(mu);
428 if (do_connect) {
429 start_connect(c);
430 }
431 if (destroy) {
432 subchannel_destroy(c);
433 }
434 if (destroy_connection != NULL) {
435 connection_destroy(destroy_connection);
436 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700437}
438
Craig Tillerff54c922015-06-26 16:57:20 -0700439static void publish_transport(grpc_subchannel *c) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700440 size_t channel_stack_size;
441 connection *con;
442 grpc_channel_stack *stk;
443 size_t num_filters;
444 const grpc_channel_filter **filters;
445 waiting_for_connect *w4c;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700446 grpc_transport_op op;
447 state_watcher *sw;
448 connection *destroy_connection = NULL;
449 grpc_channel_element *elem;
Craig Tiller5945ee12015-06-27 10:36:09 -0700450
Craig Tillerdf91ba52015-06-29 10:55:46 -0700451 /* build final filter list */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700452 num_filters = c->num_filters + c->connecting_result.num_filters + 1;
453 filters = gpr_malloc(sizeof(*filters) * num_filters);
454 memcpy(filters, c->filters, sizeof(*filters) * c->num_filters);
455 memcpy(filters + c->num_filters, c->connecting_result.filters,
456 sizeof(*filters) * c->connecting_result.num_filters);
457 filters[num_filters - 1] = &grpc_connected_channel_filter;
Craig Tiller5945ee12015-06-27 10:36:09 -0700458
Craig Tillerdf91ba52015-06-29 10:55:46 -0700459 /* construct channel stack */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700460 channel_stack_size = grpc_channel_stack_size(filters, num_filters);
461 con = gpr_malloc(sizeof(connection) + channel_stack_size);
462 stk = (grpc_channel_stack *)(con + 1);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700463 con->refs = 0;
464 con->subchannel = c;
Craig Tiller98465032015-06-29 14:36:42 -0700465 grpc_channel_stack_init(filters, num_filters, c->master, c->args, c->mdctx, stk);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700466 grpc_connected_channel_bind_transport(stk, c->connecting_result.transport);
Craig Tiller98465032015-06-29 14:36:42 -0700467 gpr_free(c->connecting_result.filters);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700468 memset(&c->connecting_result, 0, sizeof(c->connecting_result));
Craig Tillerff54c922015-06-26 16:57:20 -0700469
Craig Tillerdf91ba52015-06-29 10:55:46 -0700470 /* initialize state watcher */
471 sw = gpr_malloc(sizeof(*sw));
472 grpc_iomgr_closure_init(&sw->closure, on_state_changed, sw);
473 sw->subchannel = c;
474 sw->connectivity_state = GRPC_CHANNEL_READY;
475
Craig Tiller4ab82d22015-06-29 09:40:33 -0700476 gpr_mu_lock(&c->mu);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700477
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700478 if (c->disconnected) {
479 gpr_mu_unlock(&c->mu);
480 gpr_free(sw);
481 gpr_free(filters);
482 grpc_channel_stack_destroy(stk);
483 return;
484 }
485
Craig Tillerdf91ba52015-06-29 10:55:46 -0700486 /* publish */
487 if (c->active != NULL && c->active->refs == 0) {
488 destroy_connection = c->active;
489 }
Craig Tiller4ab82d22015-06-29 09:40:33 -0700490 c->active = con;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700491 c->active_version++;
492 sw->version = c->active_version;
Craig Tiller4ab82d22015-06-29 09:40:33 -0700493 c->connecting = 0;
Craig Tillerdf91ba52015-06-29 10:55:46 -0700494
495 /* watch for changes; subchannel ref for connecting is donated
496 to the state watcher */
497 memset(&op, 0, sizeof(op));
498 op.connectivity_state = &sw->connectivity_state;
499 op.on_connectivity_state_change = &sw->closure;
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700500 SUBCHANNEL_REF_LOCKED(c, "state_watcher");
501 GPR_ASSERT(!SUBCHANNEL_UNREF_LOCKED(c, "connecting"));
Craig Tillerf62d6fc2015-06-29 10:55:59 -0700502 elem =
503 grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
Craig Tillerdf91ba52015-06-29 10:55:46 -0700504 elem->filter->start_transport_op(elem, &op);
505
506 /* signal completion */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700507 connectivity_state_changed_locked(c);
508 while ((w4c = c->waiting)) {
Craig Tillerdf91ba52015-06-29 10:55:46 -0700509 c->waiting = w4c->next;
510 grpc_iomgr_add_callback(&w4c->continuation);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700511 }
Craig Tillerdf91ba52015-06-29 10:55:46 -0700512
Craig Tiller4ab82d22015-06-29 09:40:33 -0700513 gpr_mu_unlock(&c->mu);
Craig Tiller5945ee12015-06-27 10:36:09 -0700514
Craig Tiller4ab82d22015-06-29 09:40:33 -0700515 gpr_free(filters);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700516
Craig Tillerdf91ba52015-06-29 10:55:46 -0700517 if (destroy_connection != NULL) {
518 connection_destroy(destroy_connection);
Craig Tiller4ab82d22015-06-29 09:40:33 -0700519 }
520}
Craig Tillerff54c922015-06-26 16:57:20 -0700521
522static void subchannel_connected(void *arg, int iomgr_success) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700523 grpc_subchannel *c = arg;
524 if (c->connecting_result.transport) {
525 publish_transport(c);
526 } else {
527 int destroy;
528 gpr_mu_lock(&c->mu);
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700529 destroy = SUBCHANNEL_UNREF_LOCKED(c, "connecting");
Craig Tiller4ab82d22015-06-29 09:40:33 -0700530 gpr_mu_unlock(&c->mu);
531 if (destroy) subchannel_destroy(c);
532 /* TODO(ctiller): retry after sleeping */
533 abort();
534 }
Craig Tillerff54c922015-06-26 16:57:20 -0700535}
536
Craig Tiller5f84c842015-06-26 16:08:21 -0700537static gpr_timespec compute_connect_deadline(grpc_subchannel *c) {
538 return gpr_time_add(gpr_now(), gpr_time_from_seconds(60));
539}
540
541static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) {
Craig Tillerb6fbf1d2015-06-29 15:25:49 -0700542 if (c->disconnected) {
543 return GRPC_CHANNEL_FATAL_FAILURE;
544 }
Craig Tiller5f84c842015-06-26 16:08:21 -0700545 if (c->connecting) {
546 return GRPC_CHANNEL_CONNECTING;
547 }
548 if (c->active) {
549 return GRPC_CHANNEL_READY;
550 }
551 return GRPC_CHANNEL_IDLE;
552}
553
554static void connectivity_state_changed_locked(grpc_subchannel *c) {
555 grpc_connectivity_state current = compute_connectivity_locked(c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700556 grpc_connectivity_state_set(&c->state_tracker, current);
Craig Tiller5f84c842015-06-26 16:08:21 -0700557}
558
Craig Tiller2595ab72015-06-25 15:26:00 -0700559/*
560 * grpc_subchannel_call implementation
561 */
562
Craig Tillerc3967532015-06-29 14:59:38 -0700563void grpc_subchannel_call_ref(grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
564 gpr_ref(&c->refs);
565}
Craig Tiller2595ab72015-06-25 15:26:00 -0700566
Craig Tillerc3967532015-06-29 14:59:38 -0700567void grpc_subchannel_call_unref(grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700568 if (gpr_unref(&c->refs)) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700569 gpr_mu *mu = &c->connection->subchannel->mu;
570 grpc_subchannel *destroy;
Craig Tillerca3e9d32015-06-27 18:37:27 -0700571 grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(c));
Craig Tillerd7b68e72015-06-28 11:41:09 -0700572 gpr_mu_lock(mu);
Craig Tillerc3967532015-06-29 14:59:38 -0700573 destroy = CONNECTION_UNREF_LOCKED(c->connection, "call");
Craig Tillerd7b68e72015-06-28 11:41:09 -0700574 gpr_mu_unlock(mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700575 gpr_free(c);
Craig Tillerc3967532015-06-29 14:59:38 -0700576 if (destroy != NULL) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700577 subchannel_destroy(destroy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700578 }
Craig Tiller2595ab72015-06-25 15:26:00 -0700579 }
580}
581
582void grpc_subchannel_call_process_op(grpc_subchannel_call *call,
583 grpc_transport_stream_op *op) {
584 grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
585 grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
586 top_elem->filter->start_transport_stream_op(top_elem, op);
587}
Craig Tillereb3b12e2015-06-26 14:42:49 -0700588
Craig Tillerabf36382015-06-29 16:13:27 -0700589grpc_subchannel_call *create_call(connection *con) {
Craig Tiller4ab82d22015-06-29 09:40:33 -0700590 grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con);
591 grpc_subchannel_call *call =
592 gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700593 grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call);
594 call->connection = con;
595 gpr_ref_init(&call->refs, 1);
Craig Tillerabf36382015-06-29 16:13:27 -0700596 grpc_call_stack_init(chanstk, NULL, NULL, callstk);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700597 return call;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700598}