Craig Tiller | cd29c58 | 2015-06-24 09:15:15 -0700 | [diff] [blame] | 1 | /* |
| 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/resolvers/dns_resolver.h" |
| 35 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/string_util.h> |
| 40 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 41 | #include "src/core/client_config/lb_policies/pick_first.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 42 | #include "src/core/iomgr/resolve_address.h" |
| 43 | #include "src/core/support/string.h" |
| 44 | |
| 45 | typedef struct { |
| 46 | /** base class: must be first */ |
| 47 | grpc_resolver base; |
| 48 | /** refcount */ |
| 49 | gpr_refcount refs; |
| 50 | /** name to resolve */ |
| 51 | char *name; |
| 52 | /** default port to use */ |
| 53 | char *default_port; |
| 54 | /** subchannel factory */ |
| 55 | grpc_subchannel_factory *subchannel_factory; |
| 56 | /** load balancing policy factory */ |
| 57 | grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, |
| 58 | size_t num_subchannels); |
| 59 | |
| 60 | /** mutex guarding the rest of the state */ |
| 61 | gpr_mu mu; |
| 62 | /** are we currently resolving? */ |
| 63 | int resolving; |
| 64 | /** which version of resolved_config have we published? */ |
| 65 | int published_version; |
| 66 | /** which version of resolved_config is current? */ |
| 67 | int resolved_version; |
| 68 | /** pending next completion, or NULL */ |
| 69 | grpc_iomgr_closure *next_completion; |
| 70 | /** target config address for next completion */ |
| 71 | grpc_client_config **target_config; |
| 72 | /** current (fully resolved) config */ |
| 73 | grpc_client_config *resolved_config; |
| 74 | } dns_resolver; |
| 75 | |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 76 | static void dns_destroy(grpc_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 77 | |
| 78 | static void dns_start_resolving_locked(dns_resolver *r); |
| 79 | static void dns_maybe_finish_next_locked(dns_resolver *r); |
| 80 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 81 | static void dns_shutdown(grpc_resolver *r); |
| 82 | static void dns_channel_saw_error(grpc_resolver *r, |
| 83 | struct sockaddr *failing_address, |
| 84 | int failing_address_len); |
| 85 | static void dns_next(grpc_resolver *r, grpc_client_config **target_config, |
| 86 | grpc_iomgr_closure *on_complete); |
| 87 | |
| 88 | static const grpc_resolver_vtable dns_resolver_vtable = { |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 89 | dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 90 | |
| 91 | static void dns_shutdown(grpc_resolver *resolver) { |
| 92 | dns_resolver *r = (dns_resolver *)resolver; |
| 93 | gpr_mu_lock(&r->mu); |
| 94 | if (r->next_completion != NULL) { |
| 95 | *r->target_config = NULL; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 96 | grpc_iomgr_add_callback(r->next_completion); |
| 97 | r->next_completion = NULL; |
| 98 | } |
| 99 | gpr_mu_unlock(&r->mu); |
| 100 | } |
| 101 | |
| 102 | static void dns_channel_saw_error(grpc_resolver *resolver, struct sockaddr *sa, |
| 103 | int len) { |
| 104 | dns_resolver *r = (dns_resolver *)resolver; |
| 105 | gpr_mu_lock(&r->mu); |
| 106 | if (!r->resolving) { |
| 107 | dns_start_resolving_locked(r); |
| 108 | } |
| 109 | gpr_mu_unlock(&r->mu); |
| 110 | } |
| 111 | |
| 112 | static void dns_next(grpc_resolver *resolver, |
| 113 | grpc_client_config **target_config, |
| 114 | grpc_iomgr_closure *on_complete) { |
| 115 | dns_resolver *r = (dns_resolver *)resolver; |
| 116 | gpr_mu_lock(&r->mu); |
| 117 | GPR_ASSERT(!r->next_completion); |
| 118 | r->next_completion = on_complete; |
| 119 | r->target_config = target_config; |
| 120 | if (r->resolved_version == 0 && !r->resolving) { |
| 121 | dns_start_resolving_locked(r); |
| 122 | } else { |
| 123 | dns_maybe_finish_next_locked(r); |
| 124 | } |
| 125 | gpr_mu_unlock(&r->mu); |
| 126 | } |
| 127 | |
| 128 | static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) { |
| 129 | dns_resolver *r = arg; |
| 130 | grpc_client_config *config = NULL; |
| 131 | grpc_subchannel **subchannels; |
| 132 | grpc_subchannel_args args; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 133 | grpc_lb_policy *lb_policy; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 134 | size_t i; |
| 135 | if (addresses) { |
| 136 | config = grpc_client_config_create(); |
| 137 | subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs); |
| 138 | for (i = 0; i < addresses->naddrs; i++) { |
| 139 | memset(&args, 0, sizeof(args)); |
| 140 | args.addr = (struct sockaddr *)(addresses->addrs[i].addr); |
| 141 | args.addr_len = addresses->addrs[i].len; |
| 142 | subchannels[i] = grpc_subchannel_factory_create_subchannel( |
| 143 | r->subchannel_factory, &args); |
| 144 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 145 | lb_policy = r->lb_policy_factory(subchannels, addresses->naddrs); |
| 146 | grpc_client_config_set_lb_policy(config, lb_policy); |
| 147 | GRPC_LB_POLICY_UNREF(lb_policy, "construction"); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 148 | grpc_resolved_addresses_destroy(addresses); |
| 149 | gpr_free(subchannels); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 150 | } |
| 151 | gpr_mu_lock(&r->mu); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 152 | GPR_ASSERT(r->resolving); |
| 153 | r->resolving = 0; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 154 | if (r->resolved_config) { |
| 155 | grpc_client_config_unref(r->resolved_config); |
| 156 | } |
| 157 | r->resolved_config = config; |
| 158 | r->resolved_version++; |
| 159 | dns_maybe_finish_next_locked(r); |
| 160 | gpr_mu_unlock(&r->mu); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 161 | |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 162 | GRPC_RESOLVER_UNREF(&r->base, "dns-resolving"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void dns_start_resolving_locked(dns_resolver *r) { |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 166 | GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
| 167 | GPR_ASSERT(!r->resolving); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 168 | r->resolving = 1; |
| 169 | grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r); |
| 170 | } |
| 171 | |
| 172 | static void dns_maybe_finish_next_locked(dns_resolver *r) { |
| 173 | if (r->next_completion != NULL && |
| 174 | r->resolved_version != r->published_version) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 175 | *r->target_config = r->resolved_config; |
| 176 | grpc_client_config_ref(r->resolved_config); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 177 | grpc_iomgr_add_callback(r->next_completion); |
| 178 | r->next_completion = NULL; |
| 179 | r->published_version = r->resolved_version; |
| 180 | } |
| 181 | } |
| 182 | |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 183 | static void dns_destroy(grpc_resolver *gr) { |
| 184 | dns_resolver *r = (dns_resolver *)gr; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 185 | gpr_mu_destroy(&r->mu); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 186 | if (r->resolved_config) { |
| 187 | grpc_client_config_unref(r->resolved_config); |
| 188 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 189 | grpc_subchannel_factory_unref(r->subchannel_factory); |
| 190 | gpr_free(r->name); |
| 191 | gpr_free(r->default_port); |
| 192 | gpr_free(r); |
| 193 | } |
| 194 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 195 | static grpc_resolver *dns_create( |
| 196 | grpc_uri *uri, const char *default_port, |
| 197 | grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, |
| 198 | size_t num_subchannels), |
| 199 | grpc_subchannel_factory *subchannel_factory) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 200 | dns_resolver *r; |
| 201 | const char *path = uri->path; |
| 202 | |
| 203 | if (0 != strcmp(uri->authority, "")) { |
| 204 | gpr_log(GPR_ERROR, "authority based uri's not supported"); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | if (path[0] == '/') ++path; |
| 209 | |
| 210 | r = gpr_malloc(sizeof(dns_resolver)); |
| 211 | memset(r, 0, sizeof(*r)); |
| 212 | gpr_ref_init(&r->refs, 1); |
| 213 | gpr_mu_init(&r->mu); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame^] | 214 | grpc_resolver_init(&r->base, &dns_resolver_vtable); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 215 | r->name = gpr_strdup(path); |
| 216 | r->default_port = gpr_strdup(default_port); |
| 217 | r->subchannel_factory = subchannel_factory; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 218 | r->lb_policy_factory = lb_policy_factory; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 219 | grpc_subchannel_factory_ref(subchannel_factory); |
| 220 | return &r->base; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * FACTORY |
| 225 | */ |
| 226 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 227 | static void dns_factory_ref(grpc_resolver_factory *factory) {} |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 228 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 229 | static void dns_factory_unref(grpc_resolver_factory *factory) {} |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 230 | |
| 231 | static grpc_resolver *dns_factory_create_resolver( |
| 232 | grpc_resolver_factory *factory, grpc_uri *uri, |
| 233 | grpc_subchannel_factory *subchannel_factory) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 234 | return dns_create(uri, "https", grpc_create_pick_first_lb_policy, |
| 235 | subchannel_factory); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static const grpc_resolver_factory_vtable dns_factory_vtable = { |
| 239 | dns_factory_ref, dns_factory_unref, dns_factory_create_resolver}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 240 | static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 241 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 242 | grpc_resolver_factory *grpc_dns_resolver_factory_create() { |
| 243 | return &dns_resolver_factory; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 244 | } |