blob: 40d90be12477e972c2701b949fe2e1f22ec13577 [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller9d0e0472015-06-23 09:12:48 -07004 * 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
Craig Tiller5aceb272016-03-31 13:50:42 -070034#ifndef GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_H
35#define GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_H
Craig Tiller9d0e0472015-06-23 09:12:48 -070036
Craig Tillerd4c98332016-03-31 13:45:47 -070037#include "src/core/ext/client_config/connector.h"
Craig Tiller4a6dad32016-04-01 13:51:14 -070038#include "src/core/lib/channel/channel_stack.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070039#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/transport/connectivity_state.h"
Craig Tilleraf691802015-06-23 14:57:07 -070041
Craig Tiller9d0e0472015-06-23 09:12:48 -070042/** A (sub-)channel that knows how to connect to exactly one target
43 address. Provides a target for load balancing. */
Craig Tiller9e8f9112015-06-23 10:56:11 -070044typedef struct grpc_subchannel grpc_subchannel;
Craig Tillerb5585d42015-11-17 07:18:31 -080045typedef struct grpc_connected_subchannel grpc_connected_subchannel;
Craig Tillerf5f17122015-06-25 08:47:26 -070046typedef struct grpc_subchannel_call grpc_subchannel_call;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070047typedef struct grpc_subchannel_args grpc_subchannel_args;
Craig Tiller9d0e0472015-06-23 09:12:48 -070048
Craig Tiller577c9b22015-11-02 14:11:15 -080049#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller079a11b2015-06-30 10:07:15 -070050#define GRPC_SUBCHANNEL_REF(p, r) \
51 grpc_subchannel_ref((p), __FILE__, __LINE__, (r))
Craig Tiller7391f132016-01-22 06:39:54 -080052#define GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(p, r) \
53 grpc_subchannel_ref_from_weak_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -070054#define GRPC_SUBCHANNEL_UNREF(cl, p, r) \
55 grpc_subchannel_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tiller12ad5d62015-11-27 12:17:30 -080056#define GRPC_SUBCHANNEL_WEAK_REF(p, r) \
57 grpc_subchannel_weak_ref((p), __FILE__, __LINE__, (r))
58#define GRPC_SUBCHANNEL_WEAK_UNREF(cl, p, r) \
59 grpc_subchannel_weak_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tillerb5585d42015-11-17 07:18:31 -080060#define GRPC_CONNECTED_SUBCHANNEL_REF(p, r) \
61 grpc_connected_subchannel_ref((p), __FILE__, __LINE__, (r))
62#define GRPC_CONNECTED_SUBCHANNEL_UNREF(cl, p, r) \
63 grpc_connected_subchannel_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tiller079a11b2015-06-30 10:07:15 -070064#define GRPC_SUBCHANNEL_CALL_REF(p, r) \
65 grpc_subchannel_call_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -070066#define GRPC_SUBCHANNEL_CALL_UNREF(cl, p, r) \
67 grpc_subchannel_call_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tiller079a11b2015-06-30 10:07:15 -070068#define GRPC_SUBCHANNEL_REF_EXTRA_ARGS \
69 , const char *file, int line, const char *reason
Craig Tillerc3967532015-06-29 14:59:38 -070070#else
71#define GRPC_SUBCHANNEL_REF(p, r) grpc_subchannel_ref((p))
Craig Tiller5de79ee2016-01-25 08:16:02 -080072#define GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(p, r) \
73 grpc_subchannel_ref_from_weak_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -070074#define GRPC_SUBCHANNEL_UNREF(cl, p, r) grpc_subchannel_unref((cl), (p))
Craig Tiller12ad5d62015-11-27 12:17:30 -080075#define GRPC_SUBCHANNEL_WEAK_REF(p, r) grpc_subchannel_weak_ref((p))
Craig Tiller1d881fb2015-12-01 07:39:04 -080076#define GRPC_SUBCHANNEL_WEAK_UNREF(cl, p, r) \
77 grpc_subchannel_weak_unref((cl), (p))
Craig Tillerb5585d42015-11-17 07:18:31 -080078#define GRPC_CONNECTED_SUBCHANNEL_REF(p, r) grpc_connected_subchannel_ref((p))
Craig Tillerab33b482015-11-21 08:11:04 -080079#define GRPC_CONNECTED_SUBCHANNEL_UNREF(cl, p, r) \
80 grpc_connected_subchannel_unref((cl), (p))
Craig Tillerc3967532015-06-29 14:59:38 -070081#define GRPC_SUBCHANNEL_CALL_REF(p, r) grpc_subchannel_call_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -070082#define GRPC_SUBCHANNEL_CALL_UNREF(cl, p, r) \
83 grpc_subchannel_call_unref((cl), (p))
Craig Tillerc3967532015-06-29 14:59:38 -070084#define GRPC_SUBCHANNEL_REF_EXTRA_ARGS
85#endif
86
Craig Tillerf40df232016-03-25 13:38:14 -070087grpc_subchannel *grpc_subchannel_ref(
88 grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tiller5de79ee2016-01-25 08:16:02 -080089grpc_subchannel *grpc_subchannel_ref_from_weak_ref(
90 grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillera82950e2015-09-22 12:33:20 -070091void grpc_subchannel_unref(grpc_exec_ctx *exec_ctx,
92 grpc_subchannel *channel
Craig Tillerddf3a512015-09-24 13:03:44 -070093 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerf40df232016-03-25 13:38:14 -070094grpc_subchannel *grpc_subchannel_weak_ref(
95 grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tiller12ad5d62015-11-27 12:17:30 -080096void grpc_subchannel_weak_unref(grpc_exec_ctx *exec_ctx,
Craig Tiller1d881fb2015-12-01 07:39:04 -080097 grpc_subchannel *channel
98 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerf40df232016-03-25 13:38:14 -070099void grpc_connected_subchannel_ref(
100 grpc_connected_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerb5585d42015-11-17 07:18:31 -0800101void grpc_connected_subchannel_unref(grpc_exec_ctx *exec_ctx,
Craig Tillerab33b482015-11-21 08:11:04 -0800102 grpc_connected_subchannel *channel
103 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerf40df232016-03-25 13:38:14 -0700104void grpc_subchannel_call_ref(
105 grpc_subchannel_call *call GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillera82950e2015-09-22 12:33:20 -0700106void grpc_subchannel_call_unref(grpc_exec_ctx *exec_ctx,
107 grpc_subchannel_call *call
Craig Tillerddf3a512015-09-24 13:03:44 -0700108 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700109
Craig Tillerb5585d42015-11-17 07:18:31 -0800110/** construct a subchannel call */
Mark D. Roth09e66982016-06-23 11:14:18 -0700111grpc_error *grpc_connected_subchannel_create_call(
Craig Tillerab33b482015-11-21 08:11:04 -0800112 grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *connected_subchannel,
Mark D. Roth76d24422016-06-23 13:22:10 -0700113 grpc_polling_entity *pollent, grpc_subchannel_call **subchannel_call);
David Garcia Quintas17ee3902015-10-05 16:10:20 -0700114
Craig Tillerc7b5f762015-06-27 11:48:42 -0700115/** process a transport level op */
Craig Tillerab33b482015-11-21 08:11:04 -0800116void grpc_connected_subchannel_process_transport_op(
117 grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *subchannel,
118 grpc_transport_op *op);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700119
Craig Tiller9d0e0472015-06-23 09:12:48 -0700120/** poll the current connectivity state of a channel */
Craig Tiller9e8f9112015-06-23 10:56:11 -0700121grpc_connectivity_state grpc_subchannel_check_connectivity(
Craig Tiller804ff712016-05-05 16:25:40 -0700122 grpc_subchannel *channel, grpc_error **error);
Craig Tillerd58b5b82015-06-23 12:30:00 -0700123
Craig Tiller9d0e0472015-06-23 09:12:48 -0700124/** call notify when the connectivity state of a channel changes from *state.
125 Updates *state with the new state of the channel */
Craig Tiller1d881fb2015-12-01 07:39:04 -0800126void grpc_subchannel_notify_on_state_change(
127 grpc_exec_ctx *exec_ctx, grpc_subchannel *channel,
128 grpc_pollset_set *interested_parties, grpc_connectivity_state *state,
129 grpc_closure *notify);
Craig Tillerab33b482015-11-21 08:11:04 -0800130void grpc_connected_subchannel_notify_on_state_change(
131 grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *channel,
Craig Tillera6bebf42015-12-01 17:02:35 -0800132 grpc_pollset_set *interested_parties, grpc_connectivity_state *state,
133 grpc_closure *notify);
Craig Tillere2c62372015-12-07 16:11:03 -0800134void grpc_connected_subchannel_ping(grpc_exec_ctx *exec_ctx,
135 grpc_connected_subchannel *channel,
136 grpc_closure *notify);
Craig Tillerd58b5b82015-06-23 12:30:00 -0700137
Craig Tillerb5585d42015-11-17 07:18:31 -0800138/** retrieve the grpc_connected_subchannel - or NULL if called before
139 the subchannel becomes connected */
Craig Tillerab33b482015-11-21 08:11:04 -0800140grpc_connected_subchannel *grpc_subchannel_get_connected_subchannel(
141 grpc_subchannel *subchannel);
Craig Tillerb5585d42015-11-17 07:18:31 -0800142
Craig Tillerf5f17122015-06-25 08:47:26 -0700143/** continue processing a transport op */
Craig Tillera82950e2015-09-22 12:33:20 -0700144void grpc_subchannel_call_process_op(grpc_exec_ctx *exec_ctx,
145 grpc_subchannel_call *subchannel_call,
Craig Tillerb7959a02015-06-25 08:50:54 -0700146 grpc_transport_stream_op *op);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700147
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700148/** continue querying for peer */
Craig Tillera82950e2015-09-22 12:33:20 -0700149char *grpc_subchannel_call_get_peer(grpc_exec_ctx *exec_ctx,
150 grpc_subchannel_call *subchannel_call);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700151
Craig Tiller577c9b22015-11-02 14:11:15 -0800152grpc_call_stack *grpc_subchannel_call_get_call_stack(
153 grpc_subchannel_call *subchannel_call);
154
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700155struct grpc_subchannel_args {
Craig Tiller7391f132016-01-22 06:39:54 -0800156 /* When updating this struct, also update subchannel_index.c */
Craig Tiller5de79ee2016-01-25 08:16:02 -0800157
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700158 /** Channel filters for this channel - wrapped factories will likely
159 want to mutate this */
160 const grpc_channel_filter **filters;
161 /** The number of filters in the above array */
162 size_t filter_count;
163 /** Channel arguments to be supplied to the newly created channel */
164 const grpc_channel_args *args;
Mark D. Rothe273b032016-07-22 13:24:21 -0700165 /** Server name */
166 char *server_name; // Does not own.
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700167 /** Address to connect to */
168 struct sockaddr *addr;
169 size_t addr_len;
170};
171
Craig Tiller91624662015-06-25 16:31:02 -0700172/** create a subchannel given a connector */
Craig Tiller7391f132016-01-22 06:39:54 -0800173grpc_subchannel *grpc_subchannel_create(grpc_exec_ctx *exec_ctx,
174 grpc_connector *connector,
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700175 grpc_subchannel_args *args);
Craig Tiller91624662015-06-25 16:31:02 -0700176
Craig Tiller5aceb272016-03-31 13:50:42 -0700177#endif /* GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_H */