blob: b519ce41740b0e75d8b77a3b72c29ddb6cd17797 [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 */
72 grpc_iomgr_closure *next_completion;
73 /** 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);
82static void dns_maybe_finish_next_locked(dns_resolver *r);
83
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070084static void dns_shutdown(grpc_resolver *r);
85static void dns_channel_saw_error(grpc_resolver *r,
86 struct sockaddr *failing_address,
87 int failing_address_len);
88static void dns_next(grpc_resolver *r, grpc_client_config **target_config,
89 grpc_iomgr_closure *on_complete);
90
91static const grpc_resolver_vtable dns_resolver_vtable = {
Craig Tiller98465032015-06-29 14:36:42 -070092 dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next};
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070093
94static void dns_shutdown(grpc_resolver *resolver) {
95 dns_resolver *r = (dns_resolver *)resolver;
96 gpr_mu_lock(&r->mu);
97 if (r->next_completion != NULL) {
98 *r->target_config = NULL;
Craig Tiller06a43f52015-09-15 07:41:28 -070099 grpc_workqueue_push(r->workqueue, r->next_completion, 1);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700100 r->next_completion = NULL;
101 }
102 gpr_mu_unlock(&r->mu);
103}
104
105static void dns_channel_saw_error(grpc_resolver *resolver, struct sockaddr *sa,
106 int len) {
107 dns_resolver *r = (dns_resolver *)resolver;
108 gpr_mu_lock(&r->mu);
109 if (!r->resolving) {
110 dns_start_resolving_locked(r);
111 }
112 gpr_mu_unlock(&r->mu);
113}
114
115static void dns_next(grpc_resolver *resolver,
116 grpc_client_config **target_config,
117 grpc_iomgr_closure *on_complete) {
118 dns_resolver *r = (dns_resolver *)resolver;
119 gpr_mu_lock(&r->mu);
120 GPR_ASSERT(!r->next_completion);
121 r->next_completion = on_complete;
122 r->target_config = target_config;
123 if (r->resolved_version == 0 && !r->resolving) {
124 dns_start_resolving_locked(r);
125 } else {
126 dns_maybe_finish_next_locked(r);
127 }
128 gpr_mu_unlock(&r->mu);
129}
130
131static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) {
132 dns_resolver *r = arg;
133 grpc_client_config *config = NULL;
134 grpc_subchannel **subchannels;
135 grpc_subchannel_args args;
Craig Tillerd7b68e72015-06-28 11:41:09 -0700136 grpc_lb_policy *lb_policy;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700137 size_t i;
138 if (addresses) {
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700139 grpc_lb_policy_args lb_policy_args;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700140 config = grpc_client_config_create();
141 subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs);
142 for (i = 0; i < addresses->naddrs; i++) {
143 memset(&args, 0, sizeof(args));
144 args.addr = (struct sockaddr *)(addresses->addrs[i].addr);
Craig Tiller3121fd42015-09-10 09:56:20 -0700145 args.addr_len = (size_t)addresses->addrs[i].len;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700146 subchannels[i] = grpc_subchannel_factory_create_subchannel(
147 r->subchannel_factory, &args);
148 }
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700149 lb_policy_args.subchannels = subchannels;
150 lb_policy_args.num_subchannels = addresses->naddrs;
151 lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700152 grpc_client_config_set_lb_policy(config, lb_policy);
153 GRPC_LB_POLICY_UNREF(lb_policy, "construction");
Craig Tiller98465032015-06-29 14:36:42 -0700154 grpc_resolved_addresses_destroy(addresses);
155 gpr_free(subchannels);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700156 }
157 gpr_mu_lock(&r->mu);
Craig Tiller98465032015-06-29 14:36:42 -0700158 GPR_ASSERT(r->resolving);
159 r->resolving = 0;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700160 if (r->resolved_config) {
161 grpc_client_config_unref(r->resolved_config);
162 }
163 r->resolved_config = config;
164 r->resolved_version++;
165 dns_maybe_finish_next_locked(r);
166 gpr_mu_unlock(&r->mu);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700167
Craig Tiller98465032015-06-29 14:36:42 -0700168 GRPC_RESOLVER_UNREF(&r->base, "dns-resolving");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700169}
170
171static void dns_start_resolving_locked(dns_resolver *r) {
Craig Tiller98465032015-06-29 14:36:42 -0700172 GRPC_RESOLVER_REF(&r->base, "dns-resolving");
173 GPR_ASSERT(!r->resolving);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700174 r->resolving = 1;
175 grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r);
176}
177
178static void dns_maybe_finish_next_locked(dns_resolver *r) {
179 if (r->next_completion != NULL &&
180 r->resolved_version != r->published_version) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700181 *r->target_config = r->resolved_config;
Craig Tillerabf36382015-06-29 16:13:27 -0700182 if (r->resolved_config) {
183 grpc_client_config_ref(r->resolved_config);
184 }
Craig Tiller06a43f52015-09-15 07:41:28 -0700185 grpc_workqueue_push(r->workqueue, r->next_completion, 1);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700186 r->next_completion = NULL;
187 r->published_version = r->resolved_version;
188 }
189}
190
Craig Tiller98465032015-06-29 14:36:42 -0700191static void dns_destroy(grpc_resolver *gr) {
192 dns_resolver *r = (dns_resolver *)gr;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700193 gpr_mu_destroy(&r->mu);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700194 if (r->resolved_config) {
195 grpc_client_config_unref(r->resolved_config);
196 }
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700197 grpc_subchannel_factory_unref(r->subchannel_factory);
Craig Tiller06a43f52015-09-15 07:41:28 -0700198 grpc_workqueue_unref(r->workqueue);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700199 gpr_free(r->name);
200 gpr_free(r->default_port);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700201 gpr_free(r->lb_policy_name);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700202 gpr_free(r);
203}
204
Craig Tiller06a43f52015-09-15 07:41:28 -0700205static grpc_resolver *dns_create(grpc_resolver_args *args,
206 const char *default_port,
207 const char *lb_policy_name) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700208 dns_resolver *r;
Craig Tiller06a43f52015-09-15 07:41:28 -0700209 const char *path = args->uri->path;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700210
Craig Tiller06a43f52015-09-15 07:41:28 -0700211 if (0 != strcmp(args->uri->authority, "")) {
212 gpr_log(GPR_ERROR, "authority based dns uri's not supported");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700213 return NULL;
214 }
215
216 if (path[0] == '/') ++path;
217
218 r = gpr_malloc(sizeof(dns_resolver));
219 memset(r, 0, sizeof(*r));
220 gpr_ref_init(&r->refs, 1);
221 gpr_mu_init(&r->mu);
Craig Tiller98465032015-06-29 14:36:42 -0700222 grpc_resolver_init(&r->base, &dns_resolver_vtable);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700223 r->name = gpr_strdup(path);
224 r->default_port = gpr_strdup(default_port);
Craig Tiller06a43f52015-09-15 07:41:28 -0700225 r->subchannel_factory = args->subchannel_factory;
226 grpc_subchannel_factory_ref(r->subchannel_factory);
227 r->workqueue = args->workqueue;
228 grpc_workqueue_ref(r->workqueue);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700229 r->lb_policy_name = gpr_strdup(lb_policy_name);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700230 return &r->base;
231}
232
233/*
234 * FACTORY
235 */
236
Craig Tillereb3b12e2015-06-26 14:42:49 -0700237static void dns_factory_ref(grpc_resolver_factory *factory) {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700238
Craig Tillereb3b12e2015-06-26 14:42:49 -0700239static void dns_factory_unref(grpc_resolver_factory *factory) {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700240
241static grpc_resolver *dns_factory_create_resolver(
Craig Tiller06a43f52015-09-15 07:41:28 -0700242 grpc_resolver_factory *factory, grpc_resolver_args *args) {
243 return dns_create(args, "https", "pick_first");
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700244}
245
Craig Tillerbc85be12015-08-24 10:36:39 -0700246char *dns_factory_get_default_host_name(grpc_resolver_factory *factory,
247 grpc_uri *uri) {
248 const char *path = uri->path;
249 if (path[0] == '/') ++path;
250 return gpr_strdup(path);
251}
252
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700253static const grpc_resolver_factory_vtable dns_factory_vtable = {
Craig Tillerbc85be12015-08-24 10:36:39 -0700254 dns_factory_ref, dns_factory_unref, dns_factory_create_resolver,
255 dns_factory_get_default_host_name, "dns"};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700256static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable};
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700257
Craig Tillereb3b12e2015-06-26 14:42:49 -0700258grpc_resolver_factory *grpc_dns_resolver_factory_create() {
259 return &dns_resolver_factory;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700260}