Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 <grpc/grpc.h> |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include "src/core/channel/channel_args.h" |
| 42 | #include "src/core/channel/client_channel.h" |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 43 | #include "src/core/channel/compress_filter.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include "src/core/channel/http_client_filter.h" |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 45 | #include "src/core/client_config/resolver_registry.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 46 | #include "src/core/iomgr/tcp_client.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | #include "src/core/surface/channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include "src/core/transport/chttp2_transport.h" |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 49 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | typedef struct { |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 51 | grpc_connector base; |
| 52 | gpr_refcount refs; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 54 | grpc_iomgr_closure *notify; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 55 | grpc_connect_in_args args; |
| 56 | grpc_connect_out_args *result; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 57 | } connector; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 59 | static void connector_ref(grpc_connector *con) { |
| 60 | connector *c = (connector *)con; |
| 61 | gpr_ref(&c->refs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 64 | static void connector_unref(grpc_connector *con) { |
| 65 | connector *c = (connector *)con; |
| 66 | if (gpr_unref(&c->refs)) { |
| 67 | gpr_free(c); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | } |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 69 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 71 | static void connected(void *arg, grpc_endpoint *tcp) { |
| 72 | connector *c = arg; |
| 73 | grpc_iomgr_closure *notify; |
| 74 | if (tcp != NULL) { |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 75 | c->result->transport = grpc_create_chttp2_transport( |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 76 | c->args.channel_args, tcp, c->args.metadata_context, 1); |
| 77 | grpc_chttp2_transport_start_reading(c->result->transport, NULL, 0); |
Craig Tiller | 87cc084 | 2015-06-30 08:15:55 -0700 | [diff] [blame] | 78 | GPR_ASSERT(c->result->transport); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 79 | c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *)); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 80 | c->result->filters[0] = &grpc_http_client_filter; |
| 81 | c->result->num_filters = 1; |
Craig Tiller | 43a2b17 | 2015-02-18 17:17:51 -0800 | [diff] [blame] | 82 | } else { |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 83 | memset(c->result, 0, sizeof(*c->result)); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 84 | } |
| 85 | notify = c->notify; |
| 86 | c->notify = NULL; |
| 87 | grpc_iomgr_add_callback(notify); |
| 88 | } |
| 89 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 90 | static void connector_connect(grpc_connector *con, |
| 91 | const grpc_connect_in_args *args, |
| 92 | grpc_connect_out_args *result, |
| 93 | grpc_iomgr_closure *notify) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 94 | connector *c = (connector *)con; |
| 95 | GPR_ASSERT(c->notify == NULL); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 96 | GPR_ASSERT(notify->cb); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 97 | c->notify = notify; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 98 | c->args = *args; |
| 99 | c->result = result; |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 100 | grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr, |
| 101 | args->addr_len, args->deadline); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 104 | static const grpc_connector_vtable connector_vtable = { |
| 105 | connector_ref, connector_unref, connector_connect}; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 106 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 107 | typedef struct { |
| 108 | grpc_subchannel_factory base; |
| 109 | gpr_refcount refs; |
| 110 | grpc_mdctx *mdctx; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 111 | grpc_channel_args *merge_args; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 112 | } subchannel_factory; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 113 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 114 | static void subchannel_factory_ref(grpc_subchannel_factory *scf) { |
| 115 | subchannel_factory *f = (subchannel_factory *)scf; |
| 116 | gpr_ref(&f->refs); |
| 117 | } |
| 118 | |
| 119 | static void subchannel_factory_unref(grpc_subchannel_factory *scf) { |
| 120 | subchannel_factory *f = (subchannel_factory *)scf; |
| 121 | if (gpr_unref(&f->refs)) { |
Craig Tiller | 6806e1e | 2015-06-30 10:28:29 -0700 | [diff] [blame] | 122 | grpc_channel_args_destroy(f->merge_args); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 123 | grpc_mdctx_unref(f->mdctx); |
| 124 | gpr_free(f); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 128 | static grpc_subchannel *subchannel_factory_create_subchannel( |
| 129 | grpc_subchannel_factory *scf, grpc_subchannel_args *args) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 130 | subchannel_factory *f = (subchannel_factory *)scf; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 131 | connector *c = gpr_malloc(sizeof(*c)); |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 132 | grpc_channel_args *final_args = |
| 133 | grpc_channel_args_merge(args->args, f->merge_args); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 134 | grpc_subchannel *s; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 135 | memset(c, 0, sizeof(*c)); |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 136 | c->base.vtable = &connector_vtable; |
| 137 | gpr_ref_init(&c->refs, 1); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 138 | args->mdctx = f->mdctx; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 139 | args->args = final_args; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 140 | s = grpc_subchannel_create(&c->base, args); |
| 141 | grpc_connector_unref(&c->base); |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 142 | grpc_channel_args_destroy(final_args); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 143 | return s; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 146 | static const grpc_subchannel_factory_vtable subchannel_factory_vtable = { |
| 147 | subchannel_factory_ref, subchannel_factory_unref, |
| 148 | subchannel_factory_create_subchannel}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | |
| 150 | /* Create a client channel: |
| 151 | Asynchronously: - resolve target |
| 152 | - connect to it (trying alternatives as presented) |
| 153 | - perform handshakes */ |
| 154 | grpc_channel *grpc_channel_create(const char *target, |
| 155 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | grpc_channel *channel = NULL; |
| 157 | #define MAX_FILTERS 3 |
| 158 | const grpc_channel_filter *filters[MAX_FILTERS]; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 159 | grpc_resolver *resolver; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 160 | subchannel_factory *f; |
| 161 | grpc_mdctx *mdctx = grpc_mdctx_create(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 162 | int n = 0; |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 163 | /* TODO(census) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | if (grpc_channel_args_is_census_enabled(args)) { |
| 165 | filters[n++] = &grpc_client_census_filter; |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 166 | } */ |
David Garcia Quintas | 331d2da | 2015-06-24 16:17:47 -0700 | [diff] [blame] | 167 | filters[n++] = &grpc_compress_filter; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 168 | filters[n++] = &grpc_client_channel_filter; |
| 169 | GPR_ASSERT(n <= MAX_FILTERS); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 171 | f = gpr_malloc(sizeof(*f)); |
| 172 | f->base.vtable = &subchannel_factory_vtable; |
| 173 | gpr_ref_init(&f->refs, 1); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 174 | grpc_mdctx_ref(mdctx); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 175 | f->mdctx = mdctx; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 176 | f->merge_args = grpc_channel_args_copy(args); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 177 | resolver = grpc_resolver_create(target, &f->base); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 178 | if (!resolver) { |
| 179 | return NULL; |
| 180 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | channel = grpc_channel_create_from_filters(filters, n, args, mdctx, 1); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 183 | grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel), |
| 184 | resolver); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 185 | GRPC_RESOLVER_UNREF(resolver, "create"); |
| 186 | grpc_subchannel_factory_unref(&f->base); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | |
| 188 | return channel; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 189 | } |