blob: 8ee7e29316cb1cbfdf48a999420b199a5063d8d9 [file] [log] [blame]
Mark D. Roth71403822016-12-02 10:51:39 -08001/*
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/ext/transport/chttp2/server/chttp2_server.h"
35
36#include <grpc/grpc.h>
37
38#include <string.h>
39
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/string_util.h>
43#include <grpc/support/sync.h>
44#include <grpc/support/useful.h>
45
46#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
47#include "src/core/lib/channel/channel_args.h"
48#include "src/core/lib/channel/handshaker.h"
49#include "src/core/lib/channel/http_server_filter.h"
50#include "src/core/lib/iomgr/endpoint.h"
51#include "src/core/lib/iomgr/resolve_address.h"
52#include "src/core/lib/iomgr/tcp_server.h"
53#include "src/core/lib/surface/api_trace.h"
54#include "src/core/lib/surface/server.h"
55
56void grpc_chttp2_server_handshaker_factory_create_handshakers(
57 grpc_exec_ctx *exec_ctx,
58 grpc_chttp2_server_handshaker_factory *handshaker_factory,
59 grpc_handshake_manager *handshake_mgr) {
60 if (handshaker_factory != NULL) {
Craig Tiller73122ba2016-12-05 08:16:58 -080061 handshaker_factory->vtable->create_handshakers(exec_ctx, handshaker_factory,
62 handshake_mgr);
Mark D. Roth71403822016-12-02 10:51:39 -080063 }
64}
65
66void grpc_chttp2_server_handshaker_factory_destroy(
67 grpc_exec_ctx *exec_ctx,
68 grpc_chttp2_server_handshaker_factory *handshaker_factory) {
69 if (handshaker_factory != NULL) {
70 handshaker_factory->vtable->destroy(exec_ctx, handshaker_factory);
71 }
72}
73
Mark D. Roth71403822016-12-02 10:51:39 -080074typedef struct pending_handshake_manager_node {
75 grpc_handshake_manager *handshake_mgr;
76 struct pending_handshake_manager_node *next;
77} pending_handshake_manager_node;
78
79typedef struct {
80 grpc_server *server;
81 grpc_tcp_server *tcp_server;
82 grpc_channel_args *args;
83 grpc_chttp2_server_handshaker_factory *handshaker_factory;
84 gpr_mu mu;
85 bool shutdown;
86 grpc_closure tcp_server_shutdown_complete;
87 grpc_closure *server_destroy_listener_done;
88 pending_handshake_manager_node *pending_handshake_mgrs;
89} server_state;
90
91typedef struct {
92 server_state *server_state;
93 grpc_pollset *accepting_pollset;
94 grpc_tcp_server_acceptor *acceptor;
95 grpc_handshake_manager *handshake_mgr;
96} server_connection_state;
97
98static void pending_handshake_manager_add_locked(
99 server_state *state, grpc_handshake_manager *handshake_mgr) {
100 pending_handshake_manager_node *node = gpr_malloc(sizeof(*node));
101 node->handshake_mgr = handshake_mgr;
102 node->next = state->pending_handshake_mgrs;
103 state->pending_handshake_mgrs = node;
104}
105
106static void pending_handshake_manager_remove_locked(
107 server_state *state, grpc_handshake_manager *handshake_mgr) {
108 pending_handshake_manager_node **prev_node = &state->pending_handshake_mgrs;
109 for (pending_handshake_manager_node *node = state->pending_handshake_mgrs;
110 node != NULL; node = node->next) {
111 if (node->handshake_mgr == handshake_mgr) {
112 *prev_node = node->next;
113 gpr_free(node);
114 break;
115 }
116 prev_node = &node->next;
117 }
118}
119
120static void pending_handshake_manager_shutdown_locked(grpc_exec_ctx *exec_ctx,
121 server_state *state) {
122 pending_handshake_manager_node *prev_node = NULL;
123 for (pending_handshake_manager_node *node = state->pending_handshake_mgrs;
124 node != NULL; node = node->next) {
125 grpc_handshake_manager_shutdown(exec_ctx, node->handshake_mgr);
126 gpr_free(prev_node);
127 prev_node = node;
128 }
129 gpr_free(prev_node);
130 state->pending_handshake_mgrs = NULL;
131}
132
133static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg,
134 grpc_error *error) {
135 grpc_handshaker_args *args = arg;
136 server_connection_state *connection_state = args->user_data;
137 gpr_mu_lock(&connection_state->server_state->mu);
138 if (error != GRPC_ERROR_NONE || connection_state->server_state->shutdown) {
139 const char *error_str = grpc_error_string(error);
140 gpr_log(GPR_ERROR, "Handshaking failed: %s", error_str);
141 grpc_error_free_string(error_str);
142 if (error == GRPC_ERROR_NONE) {
143 // We were shut down after handshaking completed successfully, so
144 // destroy the endpoint here.
145 // TODO(ctiller): It is currently necessary to shutdown endpoints
146 // before destroying them, even if we know that there are no
147 // pending read/write callbacks. This should be fixed, at which
148 // point this can be removed.
149 grpc_endpoint_shutdown(exec_ctx, args->endpoint);
150 grpc_endpoint_destroy(exec_ctx, args->endpoint);
151 grpc_channel_args_destroy(args->args);
152 grpc_slice_buffer_destroy(args->read_buffer);
153 gpr_free(args->read_buffer);
154 }
155 } else {
156 grpc_transport *transport =
157 grpc_create_chttp2_transport(exec_ctx, args->args, args->endpoint, 0);
158 grpc_server_setup_transport(
159 exec_ctx, connection_state->server_state->server, transport,
160 connection_state->accepting_pollset, args->args);
161 grpc_chttp2_transport_start_reading(exec_ctx, transport, args->read_buffer);
162 grpc_channel_args_destroy(args->args);
163 }
164 pending_handshake_manager_remove_locked(connection_state->server_state,
165 connection_state->handshake_mgr);
166 gpr_mu_unlock(&connection_state->server_state->mu);
167 grpc_handshake_manager_destroy(exec_ctx, connection_state->handshake_mgr);
168 grpc_tcp_server_unref(exec_ctx, connection_state->server_state->tcp_server);
169 gpr_free(connection_state);
170}
171
172static void on_accept(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp,
173 grpc_pollset *accepting_pollset,
174 grpc_tcp_server_acceptor *acceptor) {
175 server_state *state = arg;
176 gpr_mu_lock(&state->mu);
177 if (state->shutdown) {
178 gpr_mu_unlock(&state->mu);
179 grpc_endpoint_destroy(exec_ctx, tcp);
180 return;
181 }
182 grpc_handshake_manager *handshake_mgr = grpc_handshake_manager_create();
183 pending_handshake_manager_add_locked(state, handshake_mgr);
184 gpr_mu_unlock(&state->mu);
185 grpc_tcp_server_ref(state->tcp_server);
186 server_connection_state *connection_state =
187 gpr_malloc(sizeof(*connection_state));
188 connection_state->server_state = state;
189 connection_state->accepting_pollset = accepting_pollset;
190 connection_state->acceptor = acceptor;
191 connection_state->handshake_mgr = handshake_mgr;
192 grpc_chttp2_server_handshaker_factory_create_handshakers(
193 exec_ctx, state->handshaker_factory, connection_state->handshake_mgr);
194 // TODO(roth): We should really get this timeout value from channel
195 // args instead of hard-coding it.
196 const gpr_timespec deadline = gpr_time_add(
197 gpr_now(GPR_CLOCK_MONOTONIC), gpr_time_from_seconds(120, GPR_TIMESPAN));
Craig Tiller73122ba2016-12-05 08:16:58 -0800198 grpc_handshake_manager_do_handshake(exec_ctx, connection_state->handshake_mgr,
199 tcp, state->args, deadline, acceptor,
200 on_handshake_done, connection_state);
Mark D. Roth71403822016-12-02 10:51:39 -0800201}
202
203/* Server callback: start listening on our ports */
204static void server_start_listener(grpc_exec_ctx *exec_ctx, grpc_server *server,
205 void *arg, grpc_pollset **pollsets,
206 size_t pollset_count) {
207 server_state *state = arg;
208 gpr_mu_lock(&state->mu);
209 state->shutdown = false;
210 gpr_mu_unlock(&state->mu);
211 grpc_tcp_server_start(exec_ctx, state->tcp_server, pollsets, pollset_count,
212 on_accept, state);
213}
214
215static void tcp_server_shutdown_complete(grpc_exec_ctx *exec_ctx, void *arg,
216 grpc_error *error) {
217 server_state *state = arg;
218 /* ensure all threads have unlocked */
219 gpr_mu_lock(&state->mu);
220 grpc_closure *destroy_done = state->server_destroy_listener_done;
221 GPR_ASSERT(state->shutdown);
222 pending_handshake_manager_shutdown_locked(exec_ctx, state);
223 gpr_mu_unlock(&state->mu);
224 // Flush queued work before destroying handshaker factory, since that
225 // may do a synchronous unref.
226 grpc_exec_ctx_flush(exec_ctx);
227 grpc_chttp2_server_handshaker_factory_destroy(exec_ctx,
228 state->handshaker_factory);
229 if (destroy_done != NULL) {
230 destroy_done->cb(exec_ctx, destroy_done->cb_arg, GRPC_ERROR_REF(error));
231 grpc_exec_ctx_flush(exec_ctx);
232 }
233 grpc_channel_args_destroy(state->args);
234 gpr_mu_destroy(&state->mu);
235 gpr_free(state);
236}
237
238/* Server callback: destroy the tcp listener (so we don't generate further
239 callbacks) */
240static void server_destroy_listener(grpc_exec_ctx *exec_ctx,
241 grpc_server *server, void *arg,
242 grpc_closure *destroy_done) {
243 server_state *state = arg;
244 gpr_mu_lock(&state->mu);
245 state->shutdown = true;
246 state->server_destroy_listener_done = destroy_done;
247 grpc_tcp_server *tcp_server = state->tcp_server;
248 gpr_mu_unlock(&state->mu);
249 grpc_tcp_server_shutdown_listeners(exec_ctx, tcp_server);
250 grpc_tcp_server_unref(exec_ctx, tcp_server);
251}
252
253grpc_error *grpc_chttp2_server_add_port(
254 grpc_exec_ctx *exec_ctx, grpc_server *server, const char *addr,
255 grpc_channel_args *args,
256 grpc_chttp2_server_handshaker_factory *handshaker_factory, int *port_num) {
257 grpc_resolved_addresses *resolved = NULL;
258 grpc_tcp_server *tcp_server = NULL;
259 size_t i;
260 size_t count = 0;
261 int port_temp;
262 grpc_error *err = GRPC_ERROR_NONE;
263 server_state *state = NULL;
Mark D. Rotha0bcfbb2016-12-02 12:10:25 -0800264 grpc_error **errors = NULL;
Mark D. Roth71403822016-12-02 10:51:39 -0800265
266 *port_num = -1;
267
268 /* resolve address */
269 err = grpc_blocking_resolve_address(addr, "https", &resolved);
270 if (err != GRPC_ERROR_NONE) {
271 goto error;
272 }
273 state = gpr_malloc(sizeof(*state));
274 memset(state, 0, sizeof(*state));
275 grpc_closure_init(&state->tcp_server_shutdown_complete,
276 tcp_server_shutdown_complete, state);
Craig Tiller73122ba2016-12-05 08:16:58 -0800277 err = grpc_tcp_server_create(exec_ctx, &state->tcp_server_shutdown_complete,
278 args, &tcp_server);
Mark D. Roth71403822016-12-02 10:51:39 -0800279 if (err != GRPC_ERROR_NONE) {
280 goto error;
281 }
282
283 state->server = server;
284 state->tcp_server = tcp_server;
285 state->args = args;
286 state->handshaker_factory = handshaker_factory;
287 state->shutdown = true;
288 gpr_mu_init(&state->mu);
289
290 const size_t naddrs = resolved->naddrs;
Mark D. Rotha0bcfbb2016-12-02 12:10:25 -0800291 errors = gpr_malloc(sizeof(*errors) * naddrs);
Mark D. Roth71403822016-12-02 10:51:39 -0800292 for (i = 0; i < naddrs; i++) {
293 errors[i] =
294 grpc_tcp_server_add_port(tcp_server, &resolved->addrs[i], &port_temp);
295 if (errors[i] == GRPC_ERROR_NONE) {
296 if (*port_num == -1) {
297 *port_num = port_temp;
298 } else {
299 GPR_ASSERT(*port_num == port_temp);
300 }
301 count++;
302 }
303 }
304 if (count == 0) {
305 char *msg;
306 gpr_asprintf(&msg, "No address added out of total %" PRIuPTR " resolved",
307 naddrs);
308 err = GRPC_ERROR_CREATE_REFERENCING(msg, errors, naddrs);
309 gpr_free(msg);
310 goto error;
311 } else if (count != naddrs) {
312 char *msg;
313 gpr_asprintf(&msg, "Only %" PRIuPTR
314 " addresses added out of total %" PRIuPTR " resolved",
315 count, naddrs);
316 err = GRPC_ERROR_CREATE_REFERENCING(msg, errors, naddrs);
317 gpr_free(msg);
318
319 const char *warning_message = grpc_error_string(err);
320 gpr_log(GPR_INFO, "WARNING: %s", warning_message);
321 grpc_error_free_string(warning_message);
322 /* we managed to bind some addresses: continue */
323 }
324 grpc_resolved_addresses_destroy(resolved);
325
326 /* Register with the server only upon success */
327 grpc_server_add_listener(exec_ctx, server, state, server_start_listener,
328 server_destroy_listener);
329 goto done;
330
331/* Error path: cleanup and return */
332error:
333 GPR_ASSERT(err != GRPC_ERROR_NONE);
334 if (resolved) {
335 grpc_resolved_addresses_destroy(resolved);
336 }
337 if (tcp_server) {
338 grpc_tcp_server_unref(exec_ctx, tcp_server);
Mark D. Roth0f4bbba2016-12-02 22:16:03 +0000339 } else {
Mark D. Rotha0bb3742016-12-02 22:27:26 +0000340 grpc_channel_args_destroy(args);
341 grpc_chttp2_server_handshaker_factory_destroy(exec_ctx, handshaker_factory);
Mark D. Roth0f4bbba2016-12-02 22:16:03 +0000342 gpr_free(state);
Mark D. Roth71403822016-12-02 10:51:39 -0800343 }
Mark D. Roth71403822016-12-02 10:51:39 -0800344 *port_num = 0;
345
346done:
347 if (errors != NULL) {
348 for (i = 0; i < naddrs; i++) {
349 GRPC_ERROR_UNREF(errors[i]);
350 }
351 gpr_free(errors);
352 }
353 return err;
354}