blob: 5cbb5d99716257cab2a30bf7cde01ec2515cfccb [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 Tillerc7b5f762015-06-27 11:48:42 -070042#include "src/core/channel/connectivity_state.h"
Craig Tillereb3b12e2015-06-26 14:42:49 -070043
44typedef struct {
45 gpr_refcount refs;
46 grpc_subchannel *subchannel;
47} connection;
48
Craig Tiller5f84c842015-06-26 16:08:21 -070049typedef struct waiting_for_connect {
50 struct waiting_for_connect *next;
51 grpc_iomgr_closure *notify;
52 grpc_transport_stream_op *initial_op;
53 grpc_subchannel_call **target;
54} waiting_for_connect;
55
Craig Tiller2595ab72015-06-25 15:26:00 -070056struct grpc_subchannel {
57 gpr_refcount refs;
Craig Tiller91624662015-06-25 16:31:02 -070058 grpc_connector *connector;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070059
60 /** non-transport related channel filters */
61 const grpc_channel_filter **filters;
Craig Tiller5945ee12015-06-27 10:36:09 -070062 size_t num_filters;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070063 /** channel arguments */
64 grpc_channel_args *args;
65 /** address to connect to */
66 struct sockaddr *addr;
67 size_t addr_len;
Craig Tiller5f84c842015-06-26 16:08:21 -070068 /** metadata context */
69 grpc_mdctx *mdctx;
Craig Tillereb3b12e2015-06-26 14:42:49 -070070
71 /** set during connection */
Craig Tiller04c5d4b2015-06-26 17:21:41 -070072 grpc_connect_out_args connecting_result;
Craig Tillereb3b12e2015-06-26 14:42:49 -070073
74 /** callback for connection finishing */
75 grpc_iomgr_closure connected;
76
Craig Tiller5f84c842015-06-26 16:08:21 -070077 /** pollset_set tracking who's interested in a connection
78 being setup */
79 grpc_pollset_set pollset_set;
80
Craig Tillereb3b12e2015-06-26 14:42:49 -070081 /** mutex protecting remaining elements */
82 gpr_mu mu;
83
84 /** active connection */
85 connection *active;
86 /** are we connecting */
87 int connecting;
Craig Tiller5f84c842015-06-26 16:08:21 -070088 /** things waiting for a connection */
89 waiting_for_connect *waiting;
Craig Tillerc7b5f762015-06-27 11:48:42 -070090 /** connectivity state tracking */
91 grpc_connectivity_state_tracker state_tracker;
Craig Tiller2595ab72015-06-25 15:26:00 -070092};
93
94struct grpc_subchannel_call {
Craig Tillereb3b12e2015-06-26 14:42:49 -070095 connection *connection;
Craig Tiller2595ab72015-06-25 15:26:00 -070096 gpr_refcount refs;
97};
98
Craig Tiller04c5d4b2015-06-26 17:21:41 -070099#define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1))
100#define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1))
Craig Tiller2595ab72015-06-25 15:26:00 -0700101
Craig Tillereb3b12e2015-06-26 14:42:49 -0700102static grpc_subchannel_call *create_call(connection *con, grpc_transport_stream_op *initial_op);
Craig Tiller5f84c842015-06-26 16:08:21 -0700103static void connectivity_state_changed_locked(grpc_subchannel *c);
104static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c);
105static gpr_timespec compute_connect_deadline(grpc_subchannel *c);
Craig Tillerff54c922015-06-26 16:57:20 -0700106static void subchannel_connected(void *subchannel, int iomgr_success);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700107
Craig Tiller2595ab72015-06-25 15:26:00 -0700108/*
109 * grpc_subchannel implementation
110 */
111
Craig Tillereb3b12e2015-06-26 14:42:49 -0700112void grpc_subchannel_ref(grpc_subchannel *c) { gpr_ref(&c->refs); }
Craig Tiller2595ab72015-06-25 15:26:00 -0700113
Craig Tillereb3b12e2015-06-26 14:42:49 -0700114void grpc_subchannel_unref(grpc_subchannel *c) {
115 if (gpr_unref(&c->refs)) {
116 gpr_free(c->filters);
117 grpc_channel_args_destroy(c->args);
118 gpr_free(c->addr);
Craig Tiller5f84c842015-06-26 16:08:21 -0700119 grpc_mdctx_unref(c->mdctx);
Craig Tillerff54c922015-06-26 16:57:20 -0700120 grpc_pollset_set_destroy(&c->pollset_set);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700121 grpc_connectivity_state_destroy(&c->state_tracker);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700122 gpr_free(c);
Craig Tiller2595ab72015-06-25 15:26:00 -0700123 }
124}
125
Craig Tiller5f84c842015-06-26 16:08:21 -0700126void grpc_subchannel_add_interested_party(grpc_subchannel *c,
127 grpc_pollset *pollset) {
128 grpc_pollset_set_add_pollset(&c->pollset_set, pollset);
129}
130
131void grpc_subchannel_del_interested_party(grpc_subchannel *c,
132 grpc_pollset *pollset) {
133 grpc_pollset_set_del_pollset(&c->pollset_set, pollset);
134}
135
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700136grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
137 grpc_subchannel_args *args) {
138 grpc_subchannel *c = gpr_malloc(sizeof(*c));
139 memset(c, 0, sizeof(*c));
140 gpr_ref_init(&c->refs, 1);
141 c->connector = connector;
142 grpc_connector_ref(c->connector);
Craig Tiller5945ee12015-06-27 10:36:09 -0700143 c->num_filters = args->filter_count;
144 c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700145 memcpy(c->filters, args->filters,
Craig Tiller5945ee12015-06-27 10:36:09 -0700146 sizeof(grpc_channel_filter *) * c->num_filters);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700147 c->addr = gpr_malloc(args->addr_len);
148 memcpy(c->addr, args->addr, args->addr_len);
149 c->addr_len = args->addr_len;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700150 c->args = grpc_channel_args_copy(args->args);
Craig Tiller5f84c842015-06-26 16:08:21 -0700151 c->mdctx = args->mdctx;
152 grpc_mdctx_ref(c->mdctx);
Craig Tillerff54c922015-06-26 16:57:20 -0700153 grpc_pollset_set_init(&c->pollset_set);
154 grpc_iomgr_closure_init(&c->connected, subchannel_connected, c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700155 grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700156 gpr_mu_init(&c->mu);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700157 return c;
158}
159
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700160static void start_connect(grpc_subchannel *c) {
161 grpc_connect_in_args args;
162
163 args.interested_parties = &c->pollset_set;
164 args.addr = c->addr;
165 args.addr_len = c->addr_len;
166 args.deadline = compute_connect_deadline(c);
167 args.channel_args = c->args;
168 args.metadata_context = c->mdctx;
169
170 grpc_connector_connect(c->connector, &args, &c->connecting_result, &c->connected);
171}
172
Craig Tillereb3b12e2015-06-26 14:42:49 -0700173void grpc_subchannel_create_call(grpc_subchannel *c,
Craig Tillereb3b12e2015-06-26 14:42:49 -0700174 grpc_transport_stream_op *initial_op,
175 grpc_subchannel_call **target,
176 grpc_iomgr_closure *notify) {
177 connection *con;
178 gpr_mu_lock(&c->mu);
179 if (c->active != NULL) {
180 con = c->active;
181 gpr_ref(&con->refs);
182 gpr_mu_unlock(&c->mu);
183
184 *target = create_call(con, initial_op);
185 notify->cb(notify->cb_arg, 1);
186 } else {
Craig Tiller5f84c842015-06-26 16:08:21 -0700187 waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c));
188 w4c->next = c->waiting;
189 w4c->notify = notify;
190 w4c->initial_op = initial_op;
191 w4c->target = target;
192 c->waiting = w4c;
193 grpc_subchannel_add_interested_party(c, initial_op->bind_pollset);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700194 if (!c->connecting) {
195 c->connecting = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700196 connectivity_state_changed_locked(c);
Craig Tillerff54c922015-06-26 16:57:20 -0700197 grpc_subchannel_ref(c);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700198 gpr_mu_unlock(&c->mu);
199
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700200 start_connect(c);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700201 } else {
202 gpr_mu_unlock(&c->mu);
203 }
204 }
205}
206
Craig Tiller5f84c842015-06-26 16:08:21 -0700207grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) {
208 grpc_connectivity_state state;
209 gpr_mu_lock(&c->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700210 state = grpc_connectivity_state_check(&c->state_tracker);
Craig Tiller5f84c842015-06-26 16:08:21 -0700211 gpr_mu_unlock(&c->mu);
212 return state;
213}
214
215void grpc_subchannel_notify_on_state_change(grpc_subchannel *c,
216 grpc_connectivity_state *state,
217 grpc_iomgr_closure *notify) {
Craig Tiller5f84c842015-06-26 16:08:21 -0700218 int do_connect = 0;
Craig Tiller5f84c842015-06-26 16:08:21 -0700219 gpr_mu_lock(&c->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700220 if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state, notify)) {
221 do_connect = 1;
Craig Tiller5f84c842015-06-26 16:08:21 -0700222 c->connecting = 1;
Craig Tillerff54c922015-06-26 16:57:20 -0700223 grpc_subchannel_ref(c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700224 grpc_connectivity_state_set(&c->state_tracker, compute_connectivity_locked(c));
Craig Tiller5f84c842015-06-26 16:08:21 -0700225 }
Craig Tillerc7b5f762015-06-27 11:48:42 -0700226 gpr_mu_unlock(&c->mu);
Craig Tiller5f84c842015-06-26 16:08:21 -0700227 if (do_connect) {
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700228 start_connect(c);
Craig Tiller5f84c842015-06-26 16:08:21 -0700229 }
230}
231
Craig Tillerc7b5f762015-06-27 11:48:42 -0700232void grpc_subchannel_process_transport_op(grpc_subchannel *c, grpc_transport_op *op) {
233 abort();
234}
235
Craig Tillerff54c922015-06-26 16:57:20 -0700236static void publish_transport(grpc_subchannel *c) {
Craig Tiller5945ee12015-06-27 10:36:09 -0700237 size_t channel_stack_size;
238 connection *con;
239 grpc_channel_stack *stk;
240 size_t num_filters;
241 const grpc_channel_filter **filters;
Craig Tillerff54c922015-06-26 16:57:20 -0700242 waiting_for_connect *w4c;
Craig Tiller5945ee12015-06-27 10:36:09 -0700243
244 num_filters = c->num_filters + c->connecting_result.num_filters + 1;
245 filters = gpr_malloc(sizeof(*filters) * num_filters);
246 memcpy(filters, c->filters, sizeof(*filters) * c->num_filters);
247 memcpy(filters + c->num_filters, c->connecting_result.filters, sizeof(*filters) * c->connecting_result.num_filters);
248 filters[num_filters - 1] = &grpc_connected_channel_filter;
249
250 channel_stack_size = grpc_channel_stack_size(filters, num_filters);
251 con = gpr_malloc(sizeof(connection) + channel_stack_size);
252 stk = (grpc_channel_stack *)(con + 1);
253
Craig Tillerff54c922015-06-26 16:57:20 -0700254 gpr_ref_init(&con->refs, 1);
255 con->subchannel = c;
Craig Tiller5945ee12015-06-27 10:36:09 -0700256 grpc_channel_stack_init(filters, num_filters, c->args, c->mdctx, stk);
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700257 grpc_connected_channel_bind_transport(stk, c->connecting_result.transport);
258 memset(&c->connecting_result, 0, sizeof(c->connecting_result));
Craig Tillerff54c922015-06-26 16:57:20 -0700259
260 gpr_mu_lock(&c->mu);
261 GPR_ASSERT(c->active == NULL);
262 c->active = con;
263 c->connecting = 0;
264 connectivity_state_changed_locked(c);
265 while ((w4c = c->waiting)) {
266 abort(); /* not implemented */
267 }
268 gpr_mu_unlock(&c->mu);
Craig Tiller5945ee12015-06-27 10:36:09 -0700269
270 gpr_free(filters);
Craig Tillerff54c922015-06-26 16:57:20 -0700271}
272
273static void subchannel_connected(void *arg, int iomgr_success) {
274 grpc_subchannel *c = arg;
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700275 if (c->connecting_result.transport) {
Craig Tillerff54c922015-06-26 16:57:20 -0700276 publish_transport(c);
277 } else {
278 grpc_subchannel_unref(c);
279 /* TODO(ctiller): retry after sleeping */
280 abort();
281 }
282}
283
Craig Tiller5f84c842015-06-26 16:08:21 -0700284static gpr_timespec compute_connect_deadline(grpc_subchannel *c) {
285 return gpr_time_add(gpr_now(), gpr_time_from_seconds(60));
286}
287
288static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) {
289 if (c->connecting) {
290 return GRPC_CHANNEL_CONNECTING;
291 }
292 if (c->active) {
293 return GRPC_CHANNEL_READY;
294 }
295 return GRPC_CHANNEL_IDLE;
296}
297
298static void connectivity_state_changed_locked(grpc_subchannel *c) {
299 grpc_connectivity_state current = compute_connectivity_locked(c);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700300 grpc_connectivity_state_set(&c->state_tracker, current);
Craig Tiller5f84c842015-06-26 16:08:21 -0700301}
302
Craig Tiller2595ab72015-06-25 15:26:00 -0700303/*
304 * grpc_subchannel_call implementation
305 */
306
307void grpc_subchannel_call_ref(grpc_subchannel_call *call) {
308 gpr_ref(&call->refs);
309}
310
311void grpc_subchannel_call_unref(grpc_subchannel_call *call) {
312 if (gpr_unref(&call->refs)) {
313 grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(call));
Craig Tillereb3b12e2015-06-26 14:42:49 -0700314 if (gpr_unref(&call->connection->refs)) {
315 gpr_free(call->connection);
316 }
Craig Tiller2595ab72015-06-25 15:26:00 -0700317 gpr_free(call);
318 }
319}
320
321void grpc_subchannel_call_process_op(grpc_subchannel_call *call,
322 grpc_transport_stream_op *op) {
323 grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
324 grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
325 top_elem->filter->start_transport_stream_op(top_elem, op);
326}
Craig Tillereb3b12e2015-06-26 14:42:49 -0700327
328grpc_subchannel_call *create_call(connection *con, grpc_transport_stream_op *initial_op) {
Craig Tiller04c5d4b2015-06-26 17:21:41 -0700329 grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con);
330 grpc_subchannel_call *call = gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size);
331 grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call);
332 call->connection = con;
333 gpr_ref_init(&call->refs, 1);
334 grpc_call_stack_init(chanstk, NULL, initial_op, callstk);
335 return call;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700336}