blob: 844330c0d8f4fa4b9d4fa8c2f586379b5b1b4e44 [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
Mark D. Roth65b79c82016-12-06 07:20:20 -080056void grpc_chttp2_server_handshaker_factory_add_handshakers(
Mark D. Roth71403822016-12-02 10:51:39 -080057 grpc_exec_ctx *exec_ctx,
58 grpc_chttp2_server_handshaker_factory *handshaker_factory,
59 grpc_handshake_manager *handshake_mgr) {
60 if (handshaker_factory != NULL) {
Mark D. Roth65b79c82016-12-06 07:20:20 -080061 handshaker_factory->vtable->add_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);
Mark D. Rotheed38152016-12-08 13:59:13 -0800169 gpr_free(connection_state->acceptor);
Mark D. Roth71403822016-12-02 10:51:39 -0800170 gpr_free(connection_state);
171}
172
173static void on_accept(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp,
174 grpc_pollset *accepting_pollset,
175 grpc_tcp_server_acceptor *acceptor) {
176 server_state *state = arg;
177 gpr_mu_lock(&state->mu);
178 if (state->shutdown) {
179 gpr_mu_unlock(&state->mu);
180 grpc_endpoint_destroy(exec_ctx, tcp);
181 return;
182 }
183 grpc_handshake_manager *handshake_mgr = grpc_handshake_manager_create();
184 pending_handshake_manager_add_locked(state, handshake_mgr);
185 gpr_mu_unlock(&state->mu);
186 grpc_tcp_server_ref(state->tcp_server);
187 server_connection_state *connection_state =
188 gpr_malloc(sizeof(*connection_state));
189 connection_state->server_state = state;
190 connection_state->accepting_pollset = accepting_pollset;
191 connection_state->acceptor = acceptor;
192 connection_state->handshake_mgr = handshake_mgr;
Mark D. Roth65b79c82016-12-06 07:20:20 -0800193 grpc_chttp2_server_handshaker_factory_add_handshakers(
Mark D. Roth71403822016-12-02 10:51:39 -0800194 exec_ctx, state->handshaker_factory, connection_state->handshake_mgr);
195 // TODO(roth): We should really get this timeout value from channel
196 // args instead of hard-coding it.
197 const gpr_timespec deadline = gpr_time_add(
198 gpr_now(GPR_CLOCK_MONOTONIC), gpr_time_from_seconds(120, GPR_TIMESPAN));
Craig Tiller73122ba2016-12-05 08:16:58 -0800199 grpc_handshake_manager_do_handshake(exec_ctx, connection_state->handshake_mgr,
200 tcp, state->args, deadline, acceptor,
201 on_handshake_done, connection_state);
Mark D. Roth71403822016-12-02 10:51:39 -0800202}
203
204/* Server callback: start listening on our ports */
205static void server_start_listener(grpc_exec_ctx *exec_ctx, grpc_server *server,
206 void *arg, grpc_pollset **pollsets,
207 size_t pollset_count) {
208 server_state *state = arg;
209 gpr_mu_lock(&state->mu);
210 state->shutdown = false;
211 gpr_mu_unlock(&state->mu);
212 grpc_tcp_server_start(exec_ctx, state->tcp_server, pollsets, pollset_count,
213 on_accept, state);
214}
215
216static void tcp_server_shutdown_complete(grpc_exec_ctx *exec_ctx, void *arg,
217 grpc_error *error) {
218 server_state *state = arg;
219 /* ensure all threads have unlocked */
220 gpr_mu_lock(&state->mu);
221 grpc_closure *destroy_done = state->server_destroy_listener_done;
222 GPR_ASSERT(state->shutdown);
223 pending_handshake_manager_shutdown_locked(exec_ctx, state);
224 gpr_mu_unlock(&state->mu);
225 // Flush queued work before destroying handshaker factory, since that
226 // may do a synchronous unref.
227 grpc_exec_ctx_flush(exec_ctx);
228 grpc_chttp2_server_handshaker_factory_destroy(exec_ctx,
229 state->handshaker_factory);
230 if (destroy_done != NULL) {
231 destroy_done->cb(exec_ctx, destroy_done->cb_arg, GRPC_ERROR_REF(error));
232 grpc_exec_ctx_flush(exec_ctx);
233 }
234 grpc_channel_args_destroy(state->args);
235 gpr_mu_destroy(&state->mu);
236 gpr_free(state);
237}
238
239/* Server callback: destroy the tcp listener (so we don't generate further
240 callbacks) */
241static void server_destroy_listener(grpc_exec_ctx *exec_ctx,
242 grpc_server *server, void *arg,
243 grpc_closure *destroy_done) {
244 server_state *state = arg;
245 gpr_mu_lock(&state->mu);
246 state->shutdown = true;
247 state->server_destroy_listener_done = destroy_done;
248 grpc_tcp_server *tcp_server = state->tcp_server;
249 gpr_mu_unlock(&state->mu);
250 grpc_tcp_server_shutdown_listeners(exec_ctx, tcp_server);
251 grpc_tcp_server_unref(exec_ctx, tcp_server);
252}
253
254grpc_error *grpc_chttp2_server_add_port(
255 grpc_exec_ctx *exec_ctx, grpc_server *server, const char *addr,
256 grpc_channel_args *args,
257 grpc_chttp2_server_handshaker_factory *handshaker_factory, int *port_num) {
258 grpc_resolved_addresses *resolved = NULL;
259 grpc_tcp_server *tcp_server = NULL;
260 size_t i;
261 size_t count = 0;
262 int port_temp;
263 grpc_error *err = GRPC_ERROR_NONE;
264 server_state *state = NULL;
Mark D. Rotha0bcfbb2016-12-02 12:10:25 -0800265 grpc_error **errors = NULL;
Mark D. Roth71403822016-12-02 10:51:39 -0800266
267 *port_num = -1;
268
269 /* resolve address */
270 err = grpc_blocking_resolve_address(addr, "https", &resolved);
271 if (err != GRPC_ERROR_NONE) {
272 goto error;
273 }
274 state = gpr_malloc(sizeof(*state));
275 memset(state, 0, sizeof(*state));
276 grpc_closure_init(&state->tcp_server_shutdown_complete,
277 tcp_server_shutdown_complete, state);
Craig Tiller73122ba2016-12-05 08:16:58 -0800278 err = grpc_tcp_server_create(exec_ctx, &state->tcp_server_shutdown_complete,
279 args, &tcp_server);
Mark D. Roth71403822016-12-02 10:51:39 -0800280 if (err != GRPC_ERROR_NONE) {
281 goto error;
282 }
283
284 state->server = server;
285 state->tcp_server = tcp_server;
286 state->args = args;
287 state->handshaker_factory = handshaker_factory;
288 state->shutdown = true;
289 gpr_mu_init(&state->mu);
290
291 const size_t naddrs = resolved->naddrs;
Mark D. Rotha0bcfbb2016-12-02 12:10:25 -0800292 errors = gpr_malloc(sizeof(*errors) * naddrs);
Mark D. Roth71403822016-12-02 10:51:39 -0800293 for (i = 0; i < naddrs; i++) {
294 errors[i] =
295 grpc_tcp_server_add_port(tcp_server, &resolved->addrs[i], &port_temp);
296 if (errors[i] == GRPC_ERROR_NONE) {
297 if (*port_num == -1) {
298 *port_num = port_temp;
299 } else {
300 GPR_ASSERT(*port_num == port_temp);
301 }
302 count++;
303 }
304 }
305 if (count == 0) {
306 char *msg;
307 gpr_asprintf(&msg, "No address added out of total %" PRIuPTR " resolved",
308 naddrs);
309 err = GRPC_ERROR_CREATE_REFERENCING(msg, errors, naddrs);
310 gpr_free(msg);
311 goto error;
312 } else if (count != naddrs) {
313 char *msg;
314 gpr_asprintf(&msg, "Only %" PRIuPTR
315 " addresses added out of total %" PRIuPTR " resolved",
316 count, naddrs);
317 err = GRPC_ERROR_CREATE_REFERENCING(msg, errors, naddrs);
318 gpr_free(msg);
319
320 const char *warning_message = grpc_error_string(err);
321 gpr_log(GPR_INFO, "WARNING: %s", warning_message);
322 grpc_error_free_string(warning_message);
323 /* we managed to bind some addresses: continue */
324 }
325 grpc_resolved_addresses_destroy(resolved);
326
327 /* Register with the server only upon success */
328 grpc_server_add_listener(exec_ctx, server, state, server_start_listener,
329 server_destroy_listener);
330 goto done;
331
332/* Error path: cleanup and return */
333error:
334 GPR_ASSERT(err != GRPC_ERROR_NONE);
335 if (resolved) {
336 grpc_resolved_addresses_destroy(resolved);
337 }
338 if (tcp_server) {
339 grpc_tcp_server_unref(exec_ctx, tcp_server);
Mark D. Roth0f4bbba2016-12-02 22:16:03 +0000340 } else {
Mark D. Rotha0bb3742016-12-02 22:27:26 +0000341 grpc_channel_args_destroy(args);
342 grpc_chttp2_server_handshaker_factory_destroy(exec_ctx, handshaker_factory);
Mark D. Roth0f4bbba2016-12-02 22:16:03 +0000343 gpr_free(state);
Mark D. Roth71403822016-12-02 10:51:39 -0800344 }
Mark D. Roth71403822016-12-02 10:51:39 -0800345 *port_num = 0;
346
347done:
348 if (errors != NULL) {
349 for (i = 0; i < naddrs; i++) {
350 GRPC_ERROR_UNREF(errors[i]);
351 }
352 gpr_free(errors);
353 }
354 return err;
355}