blob: 7795606e737f4f6a94e46ced34079c235d0d67e8 [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) {
61 handshaker_factory->vtable->create_handshakers(
62 exec_ctx, handshaker_factory, handshake_mgr);
63 }
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
74
75typedef struct pending_handshake_manager_node {
76 grpc_handshake_manager *handshake_mgr;
77 struct pending_handshake_manager_node *next;
78} pending_handshake_manager_node;
79
80typedef struct {
81 grpc_server *server;
82 grpc_tcp_server *tcp_server;
83 grpc_channel_args *args;
84 grpc_chttp2_server_handshaker_factory *handshaker_factory;
85 gpr_mu mu;
86 bool shutdown;
87 grpc_closure tcp_server_shutdown_complete;
88 grpc_closure *server_destroy_listener_done;
89 pending_handshake_manager_node *pending_handshake_mgrs;
90} server_state;
91
92typedef struct {
93 server_state *server_state;
94 grpc_pollset *accepting_pollset;
95 grpc_tcp_server_acceptor *acceptor;
96 grpc_handshake_manager *handshake_mgr;
97} server_connection_state;
98
99static void pending_handshake_manager_add_locked(
100 server_state *state, grpc_handshake_manager *handshake_mgr) {
101 pending_handshake_manager_node *node = gpr_malloc(sizeof(*node));
102 node->handshake_mgr = handshake_mgr;
103 node->next = state->pending_handshake_mgrs;
104 state->pending_handshake_mgrs = node;
105}
106
107static void pending_handshake_manager_remove_locked(
108 server_state *state, grpc_handshake_manager *handshake_mgr) {
109 pending_handshake_manager_node **prev_node = &state->pending_handshake_mgrs;
110 for (pending_handshake_manager_node *node = state->pending_handshake_mgrs;
111 node != NULL; node = node->next) {
112 if (node->handshake_mgr == handshake_mgr) {
113 *prev_node = node->next;
114 gpr_free(node);
115 break;
116 }
117 prev_node = &node->next;
118 }
119}
120
121static void pending_handshake_manager_shutdown_locked(grpc_exec_ctx *exec_ctx,
122 server_state *state) {
123 pending_handshake_manager_node *prev_node = NULL;
124 for (pending_handshake_manager_node *node = state->pending_handshake_mgrs;
125 node != NULL; node = node->next) {
126 grpc_handshake_manager_shutdown(exec_ctx, node->handshake_mgr);
127 gpr_free(prev_node);
128 prev_node = node;
129 }
130 gpr_free(prev_node);
131 state->pending_handshake_mgrs = NULL;
132}
133
134static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg,
135 grpc_error *error) {
136 grpc_handshaker_args *args = arg;
137 server_connection_state *connection_state = args->user_data;
138 gpr_mu_lock(&connection_state->server_state->mu);
139 if (error != GRPC_ERROR_NONE || connection_state->server_state->shutdown) {
140 const char *error_str = grpc_error_string(error);
141 gpr_log(GPR_ERROR, "Handshaking failed: %s", error_str);
142 grpc_error_free_string(error_str);
143 if (error == GRPC_ERROR_NONE) {
144 // We were shut down after handshaking completed successfully, so
145 // destroy the endpoint here.
146 // TODO(ctiller): It is currently necessary to shutdown endpoints
147 // before destroying them, even if we know that there are no
148 // pending read/write callbacks. This should be fixed, at which
149 // point this can be removed.
150 grpc_endpoint_shutdown(exec_ctx, args->endpoint);
151 grpc_endpoint_destroy(exec_ctx, args->endpoint);
152 grpc_channel_args_destroy(args->args);
153 grpc_slice_buffer_destroy(args->read_buffer);
154 gpr_free(args->read_buffer);
155 }
156 } else {
157 grpc_transport *transport =
158 grpc_create_chttp2_transport(exec_ctx, args->args, args->endpoint, 0);
159 grpc_server_setup_transport(
160 exec_ctx, connection_state->server_state->server, transport,
161 connection_state->accepting_pollset, args->args);
162 grpc_chttp2_transport_start_reading(exec_ctx, transport, args->read_buffer);
163 grpc_channel_args_destroy(args->args);
164 }
165 pending_handshake_manager_remove_locked(connection_state->server_state,
166 connection_state->handshake_mgr);
167 gpr_mu_unlock(&connection_state->server_state->mu);
168 grpc_handshake_manager_destroy(exec_ctx, connection_state->handshake_mgr);
169 grpc_tcp_server_unref(exec_ctx, connection_state->server_state->tcp_server);
170 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;
193 grpc_chttp2_server_handshaker_factory_create_handshakers(
194 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));
199 grpc_handshake_manager_do_handshake(
200 exec_ctx, connection_state->handshake_mgr, tcp, state->args, deadline,
201 acceptor, on_handshake_done, connection_state);
202}
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);
278 err =
279 grpc_tcp_server_create(exec_ctx, &state->tcp_server_shutdown_complete,
280 args, &tcp_server);
281 if (err != GRPC_ERROR_NONE) {
282 goto error;
283 }
284
285 state->server = server;
286 state->tcp_server = tcp_server;
287 state->args = args;
288 state->handshaker_factory = handshaker_factory;
289 state->shutdown = true;
290 gpr_mu_init(&state->mu);
291
292 const size_t naddrs = resolved->naddrs;
Mark D. Rotha0bcfbb2016-12-02 12:10:25 -0800293 errors = gpr_malloc(sizeof(*errors) * naddrs);
Mark D. Roth71403822016-12-02 10:51:39 -0800294 for (i = 0; i < naddrs; i++) {
295 errors[i] =
296 grpc_tcp_server_add_port(tcp_server, &resolved->addrs[i], &port_temp);
297 if (errors[i] == GRPC_ERROR_NONE) {
298 if (*port_num == -1) {
299 *port_num = port_temp;
300 } else {
301 GPR_ASSERT(*port_num == port_temp);
302 }
303 count++;
304 }
305 }
306 if (count == 0) {
307 char *msg;
308 gpr_asprintf(&msg, "No address added out of total %" PRIuPTR " resolved",
309 naddrs);
310 err = GRPC_ERROR_CREATE_REFERENCING(msg, errors, naddrs);
311 gpr_free(msg);
312 goto error;
313 } else if (count != naddrs) {
314 char *msg;
315 gpr_asprintf(&msg, "Only %" PRIuPTR
316 " addresses added out of total %" PRIuPTR " resolved",
317 count, naddrs);
318 err = GRPC_ERROR_CREATE_REFERENCING(msg, errors, naddrs);
319 gpr_free(msg);
320
321 const char *warning_message = grpc_error_string(err);
322 gpr_log(GPR_INFO, "WARNING: %s", warning_message);
323 grpc_error_free_string(warning_message);
324 /* we managed to bind some addresses: continue */
325 }
326 grpc_resolved_addresses_destroy(resolved);
327
328 /* Register with the server only upon success */
329 grpc_server_add_listener(exec_ctx, server, state, server_start_listener,
330 server_destroy_listener);
331 goto done;
332
333/* Error path: cleanup and return */
334error:
335 GPR_ASSERT(err != GRPC_ERROR_NONE);
336 if (resolved) {
337 grpc_resolved_addresses_destroy(resolved);
338 }
339 if (tcp_server) {
340 grpc_tcp_server_unref(exec_ctx, tcp_server);
Mark D. Roth0f4bbba2016-12-02 22:16:03 +0000341 } else {
Mark D. Rotha0bb3742016-12-02 22:27:26 +0000342 grpc_channel_args_destroy(args);
343 grpc_chttp2_server_handshaker_factory_destroy(exec_ctx, handshaker_factory);
Mark D. Roth0f4bbba2016-12-02 22:16:03 +0000344 gpr_free(state);
Mark D. Roth71403822016-12-02 10:51:39 -0800345 }
Mark D. Roth71403822016-12-02 10:51:39 -0800346 *port_num = 0;
347
348done:
349 if (errors != NULL) {
350 for (i = 0; i < naddrs; i++) {
351 GRPC_ERROR_UNREF(errors[i]);
352 }
353 gpr_free(errors);
354 }
355 return err;
356}