blob: a8fcbe7b0e08147bac8a9d6ed82ef26f4d57d82d [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
Craig Tiller19482442016-01-25 09:59:20 -08003 * Copyright 2015-2016, 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 Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_CLIENT_CONFIG_SUBCHANNEL_H
35#define GRPC_CORE_LIB_CLIENT_CONFIG_SUBCHANNEL_H
Craig Tiller9d0e0472015-06-23 09:12:48 -070036
Craig Tillerf5f17122015-06-25 08:47:26 -070037#include "src/core/channel/channel_stack.h"
Craig Tiller91624662015-06-25 16:31:02 -070038#include "src/core/client_config/connector.h"
Craig Tiller5795da72015-09-17 15:27:13 -070039#include "src/core/transport/connectivity_state.h"
Craig Tilleraf691802015-06-23 14:57:07 -070040
Craig Tiller9d0e0472015-06-23 09:12:48 -070041/** A (sub-)channel that knows how to connect to exactly one target
42 address. Provides a target for load balancing. */
Craig Tiller9e8f9112015-06-23 10:56:11 -070043typedef struct grpc_subchannel grpc_subchannel;
Craig Tillerb5585d42015-11-17 07:18:31 -080044typedef struct grpc_connected_subchannel grpc_connected_subchannel;
Craig Tillerf5f17122015-06-25 08:47:26 -070045typedef struct grpc_subchannel_call grpc_subchannel_call;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070046typedef struct grpc_subchannel_args grpc_subchannel_args;
Craig Tiller9d0e0472015-06-23 09:12:48 -070047
Craig Tiller577c9b22015-11-02 14:11:15 -080048#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller079a11b2015-06-30 10:07:15 -070049#define GRPC_SUBCHANNEL_REF(p, r) \
50 grpc_subchannel_ref((p), __FILE__, __LINE__, (r))
Craig Tiller7391f132016-01-22 06:39:54 -080051#define GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(p, r) \
52 grpc_subchannel_ref_from_weak_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -070053#define GRPC_SUBCHANNEL_UNREF(cl, p, r) \
54 grpc_subchannel_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tiller12ad5d62015-11-27 12:17:30 -080055#define GRPC_SUBCHANNEL_WEAK_REF(p, r) \
56 grpc_subchannel_weak_ref((p), __FILE__, __LINE__, (r))
57#define GRPC_SUBCHANNEL_WEAK_UNREF(cl, p, r) \
58 grpc_subchannel_weak_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tillerb5585d42015-11-17 07:18:31 -080059#define GRPC_CONNECTED_SUBCHANNEL_REF(p, r) \
60 grpc_connected_subchannel_ref((p), __FILE__, __LINE__, (r))
61#define GRPC_CONNECTED_SUBCHANNEL_UNREF(cl, p, r) \
62 grpc_connected_subchannel_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tiller079a11b2015-06-30 10:07:15 -070063#define GRPC_SUBCHANNEL_CALL_REF(p, r) \
64 grpc_subchannel_call_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -070065#define GRPC_SUBCHANNEL_CALL_UNREF(cl, p, r) \
66 grpc_subchannel_call_unref((cl), (p), __FILE__, __LINE__, (r))
Craig Tiller079a11b2015-06-30 10:07:15 -070067#define GRPC_SUBCHANNEL_REF_EXTRA_ARGS \
68 , const char *file, int line, const char *reason
Craig Tillerc3967532015-06-29 14:59:38 -070069#else
70#define GRPC_SUBCHANNEL_REF(p, r) grpc_subchannel_ref((p))
Craig Tiller5de79ee2016-01-25 08:16:02 -080071#define GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(p, r) \
72 grpc_subchannel_ref_from_weak_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -070073#define GRPC_SUBCHANNEL_UNREF(cl, p, r) grpc_subchannel_unref((cl), (p))
Craig Tiller12ad5d62015-11-27 12:17:30 -080074#define GRPC_SUBCHANNEL_WEAK_REF(p, r) grpc_subchannel_weak_ref((p))
Craig Tiller1d881fb2015-12-01 07:39:04 -080075#define GRPC_SUBCHANNEL_WEAK_UNREF(cl, p, r) \
76 grpc_subchannel_weak_unref((cl), (p))
Craig Tillerb5585d42015-11-17 07:18:31 -080077#define GRPC_CONNECTED_SUBCHANNEL_REF(p, r) grpc_connected_subchannel_ref((p))
Craig Tillerab33b482015-11-21 08:11:04 -080078#define GRPC_CONNECTED_SUBCHANNEL_UNREF(cl, p, r) \
79 grpc_connected_subchannel_unref((cl), (p))
Craig Tillerc3967532015-06-29 14:59:38 -070080#define GRPC_SUBCHANNEL_CALL_REF(p, r) grpc_subchannel_call_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -070081#define GRPC_SUBCHANNEL_CALL_UNREF(cl, p, r) \
82 grpc_subchannel_call_unref((cl), (p))
Craig Tillerc3967532015-06-29 14:59:38 -070083#define GRPC_SUBCHANNEL_REF_EXTRA_ARGS
84#endif
85
Craig Tillerf40df232016-03-25 13:38:14 -070086grpc_subchannel *grpc_subchannel_ref(
87 grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tiller5de79ee2016-01-25 08:16:02 -080088grpc_subchannel *grpc_subchannel_ref_from_weak_ref(
89 grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillera82950e2015-09-22 12:33:20 -070090void grpc_subchannel_unref(grpc_exec_ctx *exec_ctx,
91 grpc_subchannel *channel
Craig Tillerddf3a512015-09-24 13:03:44 -070092 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerf40df232016-03-25 13:38:14 -070093grpc_subchannel *grpc_subchannel_weak_ref(
94 grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tiller12ad5d62015-11-27 12:17:30 -080095void grpc_subchannel_weak_unref(grpc_exec_ctx *exec_ctx,
Craig Tiller1d881fb2015-12-01 07:39:04 -080096 grpc_subchannel *channel
97 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerf40df232016-03-25 13:38:14 -070098void grpc_connected_subchannel_ref(
99 grpc_connected_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerb5585d42015-11-17 07:18:31 -0800100void grpc_connected_subchannel_unref(grpc_exec_ctx *exec_ctx,
Craig Tillerab33b482015-11-21 08:11:04 -0800101 grpc_connected_subchannel *channel
102 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillerf40df232016-03-25 13:38:14 -0700103void grpc_subchannel_call_ref(
104 grpc_subchannel_call *call GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tillera82950e2015-09-22 12:33:20 -0700105void grpc_subchannel_call_unref(grpc_exec_ctx *exec_ctx,
106 grpc_subchannel_call *call
Craig Tillerddf3a512015-09-24 13:03:44 -0700107 GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700108
Craig Tillerb5585d42015-11-17 07:18:31 -0800109/** construct a subchannel call */
Craig Tillerab33b482015-11-21 08:11:04 -0800110grpc_subchannel_call *grpc_connected_subchannel_create_call(
111 grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *connected_subchannel,
112 grpc_pollset *pollset);
David Garcia Quintas17ee3902015-10-05 16:10:20 -0700113
Craig Tillerc7b5f762015-06-27 11:48:42 -0700114/** process a transport level op */
Craig Tillerab33b482015-11-21 08:11:04 -0800115void grpc_connected_subchannel_process_transport_op(
116 grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *subchannel,
117 grpc_transport_op *op);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700118
Craig Tiller9d0e0472015-06-23 09:12:48 -0700119/** poll the current connectivity state of a channel */
Craig Tiller9e8f9112015-06-23 10:56:11 -0700120grpc_connectivity_state grpc_subchannel_check_connectivity(
121 grpc_subchannel *channel);
Craig Tillerd58b5b82015-06-23 12:30:00 -0700122
Craig Tiller9d0e0472015-06-23 09:12:48 -0700123/** call notify when the connectivity state of a channel changes from *state.
124 Updates *state with the new state of the channel */
Craig Tiller1d881fb2015-12-01 07:39:04 -0800125void grpc_subchannel_notify_on_state_change(
126 grpc_exec_ctx *exec_ctx, grpc_subchannel *channel,
127 grpc_pollset_set *interested_parties, grpc_connectivity_state *state,
128 grpc_closure *notify);
Craig Tillerab33b482015-11-21 08:11:04 -0800129void grpc_connected_subchannel_notify_on_state_change(
130 grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *channel,
Craig Tillera6bebf42015-12-01 17:02:35 -0800131 grpc_pollset_set *interested_parties, grpc_connectivity_state *state,
132 grpc_closure *notify);
Craig Tillere2c62372015-12-07 16:11:03 -0800133void grpc_connected_subchannel_ping(grpc_exec_ctx *exec_ctx,
134 grpc_connected_subchannel *channel,
135 grpc_closure *notify);
Craig Tillerd58b5b82015-06-23 12:30:00 -0700136
Craig Tillerb5585d42015-11-17 07:18:31 -0800137/** retrieve the grpc_connected_subchannel - or NULL if called before
138 the subchannel becomes connected */
Craig Tillerab33b482015-11-21 08:11:04 -0800139grpc_connected_subchannel *grpc_subchannel_get_connected_subchannel(
140 grpc_subchannel *subchannel);
Craig Tillerb5585d42015-11-17 07:18:31 -0800141
Craig Tillerf5f17122015-06-25 08:47:26 -0700142/** continue processing a transport op */
Craig Tillera82950e2015-09-22 12:33:20 -0700143void grpc_subchannel_call_process_op(grpc_exec_ctx *exec_ctx,
144 grpc_subchannel_call *subchannel_call,
Craig Tillerb7959a02015-06-25 08:50:54 -0700145 grpc_transport_stream_op *op);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700146
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700147/** continue querying for peer */
Craig Tillera82950e2015-09-22 12:33:20 -0700148char *grpc_subchannel_call_get_peer(grpc_exec_ctx *exec_ctx,
149 grpc_subchannel_call *subchannel_call);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700150
Craig Tiller577c9b22015-11-02 14:11:15 -0800151grpc_call_stack *grpc_subchannel_call_get_call_stack(
152 grpc_subchannel_call *subchannel_call);
153
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700154struct grpc_subchannel_args {
Craig Tiller7391f132016-01-22 06:39:54 -0800155 /* When updating this struct, also update subchannel_index.c */
Craig Tiller5de79ee2016-01-25 08:16:02 -0800156
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700157 /** Channel filters for this channel - wrapped factories will likely
158 want to mutate this */
159 const grpc_channel_filter **filters;
160 /** The number of filters in the above array */
161 size_t filter_count;
162 /** Channel arguments to be supplied to the newly created channel */
163 const grpc_channel_args *args;
164 /** Address to connect to */
165 struct sockaddr *addr;
166 size_t addr_len;
167};
168
Craig Tiller91624662015-06-25 16:31:02 -0700169/** create a subchannel given a connector */
Craig Tiller7391f132016-01-22 06:39:54 -0800170grpc_subchannel *grpc_subchannel_create(grpc_exec_ctx *exec_ctx,
171 grpc_connector *connector,
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700172 grpc_subchannel_args *args);
Craig Tiller91624662015-06-25 16:31:02 -0700173
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700174#endif /* GRPC_CORE_LIB_CLIENT_CONFIG_SUBCHANNEL_H */