blob: e251664e03eab7cda7ad7210bbb0fcdc87d4445d [file] [log] [blame]
Craig Tillercd29c582015-06-24 09:15:15 -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/resolvers/dns_resolver.h"
35
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070036#include <string.h>
37
38#include <grpc/support/alloc.h>
Craig Tiller6b9f5c62015-07-29 18:47:35 -070039#include <grpc/support/host_port.h>
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070040#include <grpc/support/string_util.h>
41
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070042#include "src/core/client_config/lb_policy_registry.h"
Craig Tiller6b9f5c62015-07-29 18:47:35 -070043#include "src/core/client_config/subchannel_factory_decorators/add_channel_arg.h"
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070044#include "src/core/iomgr/resolve_address.h"
45#include "src/core/support/string.h"
46
47typedef struct {
48 /** base class: must be first */
49 grpc_resolver base;
50 /** refcount */
51 gpr_refcount refs;
Craig Tiller06a43f52015-09-15 07:41:28 -070052 /** workqueue */
53 grpc_workqueue *workqueue;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070054 /** name to resolve */
55 char *name;
56 /** default port to use */
57 char *default_port;
58 /** subchannel factory */
59 grpc_subchannel_factory *subchannel_factory;
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070060 /** load balancing policy name */
61 char *lb_policy_name;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070062
63 /** mutex guarding the rest of the state */
64 gpr_mu mu;
65 /** are we currently resolving? */
66 int resolving;
67 /** which version of resolved_config have we published? */
68 int published_version;
69 /** which version of resolved_config is current? */
70 int resolved_version;
71 /** pending next completion, or NULL */
Craig Tiller33825112015-09-18 07:44:19 -070072 grpc_closure *next_completion;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070073 /** target config address for next completion */
74 grpc_client_config **target_config;
75 /** current (fully resolved) config */
76 grpc_client_config *resolved_config;
77} dns_resolver;
78
Craig Tiller98465032015-06-29 14:36:42 -070079static void dns_destroy(grpc_resolver *r);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070080
81static void dns_start_resolving_locked(dns_resolver *r);
Craig Tiller33825112015-09-18 07:44:19 -070082static grpc_closure *dns_maybe_finish_next_locked(dns_resolver *r)
Craig Tiller5795da72015-09-17 15:27:13 -070083 GRPC_MUST_USE_RESULT;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070084
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070085static void dns_shutdown(grpc_resolver *r);
86static void dns_channel_saw_error(grpc_resolver *r,
87 struct sockaddr *failing_address,
88 int failing_address_len);
89static void dns_next(grpc_resolver *r, grpc_client_config **target_config,
Craig Tiller33825112015-09-18 07:44:19 -070090 grpc_closure *on_complete);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070091
92static const grpc_resolver_vtable dns_resolver_vtable = {
Craig Tiller98465032015-06-29 14:36:42 -070093 dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next};
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070094
95static void dns_shutdown(grpc_resolver *resolver) {
96 dns_resolver *r = (dns_resolver *)resolver;
Craig Tiller33825112015-09-18 07:44:19 -070097 grpc_closure *next_completion;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070098 gpr_mu_lock(&r->mu);
Craig Tiller5795da72015-09-17 15:27:13 -070099 next_completion = r->next_completion;
100 r->next_completion = NULL;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700101 gpr_mu_unlock(&r->mu);
Craig Tiller5795da72015-09-17 15:27:13 -0700102 if (next_completion != NULL) {
103 *r->target_config = NULL;
104 next_completion->cb(next_completion->cb_arg, 1);
105 }
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700106}
107
108static void dns_channel_saw_error(grpc_resolver *resolver, struct sockaddr *sa,
109 int len) {
110 dns_resolver *r = (dns_resolver *)resolver;
111 gpr_mu_lock(&r->mu);
112 if (!r->resolving) {
113 dns_start_resolving_locked(r);
114 }
115 gpr_mu_unlock(&r->mu);
116}
117
118static void dns_next(grpc_resolver *resolver,
119 grpc_client_config **target_config,
Craig Tiller33825112015-09-18 07:44:19 -0700120 grpc_closure *on_complete) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700121 dns_resolver *r = (dns_resolver *)resolver;
Craig Tiller33825112015-09-18 07:44:19 -0700122 grpc_closure *call = NULL;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700123 gpr_mu_lock(&r->mu);
124 GPR_ASSERT(!r->next_completion);
125 r->next_completion = on_complete;
126 r->target_config = target_config;
127 if (r->resolved_version == 0 && !r->resolving) {
128 dns_start_resolving_locked(r);
129 } else {
Craig Tiller5795da72015-09-17 15:27:13 -0700130 call = dns_maybe_finish_next_locked(r);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700131 }
132 gpr_mu_unlock(&r->mu);
Craig Tiller5795da72015-09-17 15:27:13 -0700133 if (call) {
134 call->cb(call->cb_arg, 1);
135 }
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700136}
137
138static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) {
139 dns_resolver *r = arg;
140 grpc_client_config *config = NULL;
141 grpc_subchannel **subchannels;
142 grpc_subchannel_args args;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700143 grpc_lb_policy *lb_policy;
Craig Tiller33825112015-09-18 07:44:19 -0700144 grpc_closure *call;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700145 size_t i;
146 if (addresses) {
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700147 grpc_lb_policy_args lb_policy_args;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700148 config = grpc_client_config_create();
149 subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs);
150 for (i = 0; i < addresses->naddrs; i++) {
151 memset(&args, 0, sizeof(args));
152 args.addr = (struct sockaddr *)(addresses->addrs[i].addr);
Craig Tiller3121fd42015-09-10 09:56:20 -0700153 args.addr_len = (size_t)addresses->addrs[i].len;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700154 subchannels[i] = grpc_subchannel_factory_create_subchannel(
155 r->subchannel_factory, &args);
156 }
Craig Tiller8f3addc2015-09-16 10:12:12 -0700157 memset(&lb_policy_args, 0, sizeof(lb_policy_args));
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700158 lb_policy_args.subchannels = subchannels;
159 lb_policy_args.num_subchannels = addresses->naddrs;
Craig Tiller8f3addc2015-09-16 10:12:12 -0700160 lb_policy_args.workqueue = r->workqueue;
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700161 lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700162 grpc_client_config_set_lb_policy(config, lb_policy);
163 GRPC_LB_POLICY_UNREF(lb_policy, "construction");
Craig Tiller98465032015-06-29 14:36:42 -0700164 grpc_resolved_addresses_destroy(addresses);
165 gpr_free(subchannels);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700166 }
167 gpr_mu_lock(&r->mu);
Craig Tiller98465032015-06-29 14:36:42 -0700168 GPR_ASSERT(r->resolving);
169 r->resolving = 0;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700170 if (r->resolved_config) {
171 grpc_client_config_unref(r->resolved_config);
172 }
173 r->resolved_config = config;
174 r->resolved_version++;
Craig Tiller5795da72015-09-17 15:27:13 -0700175 call = dns_maybe_finish_next_locked(r);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700176 gpr_mu_unlock(&r->mu);
Craig Tiller5795da72015-09-17 15:27:13 -0700177 if (call) {
178 call->cb(call->cb_arg, 1);
179 }
Craig Tillerd7b68e72015-06-28 11:41:09 -0700180
Craig Tiller98465032015-06-29 14:36:42 -0700181 GRPC_RESOLVER_UNREF(&r->base, "dns-resolving");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700182}
183
184static void dns_start_resolving_locked(dns_resolver *r) {
Craig Tiller98465032015-06-29 14:36:42 -0700185 GRPC_RESOLVER_REF(&r->base, "dns-resolving");
186 GPR_ASSERT(!r->resolving);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700187 r->resolving = 1;
188 grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r);
189}
190
Craig Tiller33825112015-09-18 07:44:19 -0700191static grpc_closure *dns_maybe_finish_next_locked(dns_resolver *r) {
192 grpc_closure *ret = NULL;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700193 if (r->next_completion != NULL &&
194 r->resolved_version != r->published_version) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700195 *r->target_config = r->resolved_config;
Craig Tillerabf36382015-06-29 16:13:27 -0700196 if (r->resolved_config) {
197 grpc_client_config_ref(r->resolved_config);
198 }
Craig Tiller5795da72015-09-17 15:27:13 -0700199 ret = r->next_completion;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700200 r->next_completion = NULL;
201 r->published_version = r->resolved_version;
202 }
Craig Tiller5795da72015-09-17 15:27:13 -0700203 return ret;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700204}
205
Craig Tiller98465032015-06-29 14:36:42 -0700206static void dns_destroy(grpc_resolver *gr) {
207 dns_resolver *r = (dns_resolver *)gr;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700208 gpr_mu_destroy(&r->mu);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700209 if (r->resolved_config) {
210 grpc_client_config_unref(r->resolved_config);
211 }
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700212 grpc_subchannel_factory_unref(r->subchannel_factory);
Craig Tiller3cd6a512015-09-16 16:15:47 -0700213 GRPC_WORKQUEUE_UNREF(r->workqueue, "dns");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700214 gpr_free(r->name);
215 gpr_free(r->default_port);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700216 gpr_free(r->lb_policy_name);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700217 gpr_free(r);
218}
219
Craig Tiller06a43f52015-09-15 07:41:28 -0700220static grpc_resolver *dns_create(grpc_resolver_args *args,
221 const char *default_port,
222 const char *lb_policy_name) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700223 dns_resolver *r;
Craig Tiller06a43f52015-09-15 07:41:28 -0700224 const char *path = args->uri->path;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700225
Craig Tiller06a43f52015-09-15 07:41:28 -0700226 if (0 != strcmp(args->uri->authority, "")) {
227 gpr_log(GPR_ERROR, "authority based dns uri's not supported");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700228 return NULL;
229 }
230
231 if (path[0] == '/') ++path;
232
233 r = gpr_malloc(sizeof(dns_resolver));
234 memset(r, 0, sizeof(*r));
235 gpr_ref_init(&r->refs, 1);
236 gpr_mu_init(&r->mu);
Craig Tiller98465032015-06-29 14:36:42 -0700237 grpc_resolver_init(&r->base, &dns_resolver_vtable);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700238 r->name = gpr_strdup(path);
239 r->default_port = gpr_strdup(default_port);
Craig Tiller06a43f52015-09-15 07:41:28 -0700240 r->subchannel_factory = args->subchannel_factory;
241 grpc_subchannel_factory_ref(r->subchannel_factory);
242 r->workqueue = args->workqueue;
Craig Tiller3cd6a512015-09-16 16:15:47 -0700243 GRPC_WORKQUEUE_REF(r->workqueue, "dns");
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700244 r->lb_policy_name = gpr_strdup(lb_policy_name);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700245 return &r->base;
246}
247
248/*
249 * FACTORY
250 */
251
Craig Tillereb3b12e2015-06-26 14:42:49 -0700252static void dns_factory_ref(grpc_resolver_factory *factory) {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700253
Craig Tillereb3b12e2015-06-26 14:42:49 -0700254static void dns_factory_unref(grpc_resolver_factory *factory) {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700255
256static grpc_resolver *dns_factory_create_resolver(
Craig Tiller06a43f52015-09-15 07:41:28 -0700257 grpc_resolver_factory *factory, grpc_resolver_args *args) {
258 return dns_create(args, "https", "pick_first");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700259}
260
Craig Tillerbc85be12015-08-24 10:36:39 -0700261char *dns_factory_get_default_host_name(grpc_resolver_factory *factory,
262 grpc_uri *uri) {
263 const char *path = uri->path;
264 if (path[0] == '/') ++path;
265 return gpr_strdup(path);
266}
267
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700268static const grpc_resolver_factory_vtable dns_factory_vtable = {
Craig Tillerbc85be12015-08-24 10:36:39 -0700269 dns_factory_ref, dns_factory_unref, dns_factory_create_resolver,
270 dns_factory_get_default_host_name, "dns"};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700271static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable};
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700272
Craig Tillereb3b12e2015-06-26 14:42:49 -0700273grpc_resolver_factory *grpc_dns_resolver_factory_create() {
274 return &dns_resolver_factory;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700275}