Home | History | Annotate | Download | only in server
      1 /*
      2  *
      3  * Copyright 2015 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #include <grpc/support/port_platform.h>
     20 
     21 #include "src/core/ext/transport/chttp2/server/chttp2_server.h"
     22 
     23 #include <grpc/grpc.h>
     24 
     25 #include <inttypes.h>
     26 #include <limits.h>
     27 #include <string.h>
     28 
     29 #include <grpc/support/alloc.h>
     30 #include <grpc/support/log.h>
     31 #include <grpc/support/string_util.h>
     32 #include <grpc/support/sync.h>
     33 
     34 #include "src/core/ext/filters/http/server/http_server_filter.h"
     35 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
     36 #include "src/core/ext/transport/chttp2/transport/internal.h"
     37 #include "src/core/lib/channel/channel_args.h"
     38 #include "src/core/lib/channel/handshaker.h"
     39 #include "src/core/lib/channel/handshaker_registry.h"
     40 #include "src/core/lib/iomgr/endpoint.h"
     41 #include "src/core/lib/iomgr/resolve_address.h"
     42 #include "src/core/lib/iomgr/tcp_server.h"
     43 #include "src/core/lib/slice/slice_internal.h"
     44 #include "src/core/lib/surface/api_trace.h"
     45 #include "src/core/lib/surface/server.h"
     46 
     47 typedef struct {
     48   grpc_server* server;
     49   grpc_tcp_server* tcp_server;
     50   grpc_channel_args* args;
     51   gpr_mu mu;
     52   bool shutdown;
     53   grpc_closure tcp_server_shutdown_complete;
     54   grpc_closure* server_destroy_listener_done;
     55   grpc_handshake_manager* pending_handshake_mgrs;
     56 } server_state;
     57 
     58 typedef struct {
     59   gpr_refcount refs;
     60   server_state* svr_state;
     61   grpc_pollset* accepting_pollset;
     62   grpc_tcp_server_acceptor* acceptor;
     63   grpc_handshake_manager* handshake_mgr;
     64   // State for enforcing handshake timeout on receiving HTTP/2 settings.
     65   grpc_chttp2_transport* transport;
     66   grpc_millis deadline;
     67   grpc_timer timer;
     68   grpc_closure on_timeout;
     69   grpc_closure on_receive_settings;
     70 } server_connection_state;
     71 
     72 static void server_connection_state_unref(
     73     server_connection_state* connection_state) {
     74   if (gpr_unref(&connection_state->refs)) {
     75     if (connection_state->transport != nullptr) {
     76       GRPC_CHTTP2_UNREF_TRANSPORT(connection_state->transport,
     77                                   "receive settings timeout");
     78     }
     79     gpr_free(connection_state);
     80   }
     81 }
     82 
     83 static void on_timeout(void* arg, grpc_error* error) {
     84   server_connection_state* connection_state =
     85       static_cast<server_connection_state*>(arg);
     86   // Note that we may be called with GRPC_ERROR_NONE when the timer fires
     87   // or with an error indicating that the timer system is being shut down.
     88   if (error != GRPC_ERROR_CANCELLED) {
     89     grpc_transport_op* op = grpc_make_transport_op(nullptr);
     90     op->disconnect_with_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
     91         "Did not receive HTTP/2 settings before handshake timeout");
     92     grpc_transport_perform_op(&connection_state->transport->base, op);
     93   }
     94   server_connection_state_unref(connection_state);
     95 }
     96 
     97 static void on_receive_settings(void* arg, grpc_error* error) {
     98   server_connection_state* connection_state =
     99       static_cast<server_connection_state*>(arg);
    100   if (error == GRPC_ERROR_NONE) {
    101     grpc_timer_cancel(&connection_state->timer);
    102   }
    103   server_connection_state_unref(connection_state);
    104 }
    105 
    106 static void on_handshake_done(void* arg, grpc_error* error) {
    107   grpc_handshaker_args* args = static_cast<grpc_handshaker_args*>(arg);
    108   server_connection_state* connection_state =
    109       static_cast<server_connection_state*>(args->user_data);
    110   gpr_mu_lock(&connection_state->svr_state->mu);
    111   if (error != GRPC_ERROR_NONE || connection_state->svr_state->shutdown) {
    112     const char* error_str = grpc_error_string(error);
    113     gpr_log(GPR_DEBUG, "Handshaking failed: %s", error_str);
    114     if (error == GRPC_ERROR_NONE && args->endpoint != nullptr) {
    115       // We were shut down after handshaking completed successfully, so
    116       // destroy the endpoint here.
    117       // TODO(ctiller): It is currently necessary to shutdown endpoints
    118       // before destroying them, even if we know that there are no
    119       // pending read/write callbacks.  This should be fixed, at which
    120       // point this can be removed.
    121       grpc_endpoint_shutdown(args->endpoint, GRPC_ERROR_NONE);
    122       grpc_endpoint_destroy(args->endpoint);
    123       grpc_channel_args_destroy(args->args);
    124       grpc_slice_buffer_destroy_internal(args->read_buffer);
    125       gpr_free(args->read_buffer);
    126     }
    127   } else {
    128     // If the handshaking succeeded but there is no endpoint, then the
    129     // handshaker may have handed off the connection to some external
    130     // code, so we can just clean up here without creating a transport.
    131     if (args->endpoint != nullptr) {
    132       grpc_transport* transport =
    133           grpc_create_chttp2_transport(args->args, args->endpoint, false);
    134       grpc_server_setup_transport(
    135           connection_state->svr_state->server, transport,
    136           connection_state->accepting_pollset, args->args);
    137       // Use notify_on_receive_settings callback to enforce the
    138       // handshake deadline.
    139       connection_state->transport =
    140           reinterpret_cast<grpc_chttp2_transport*>(transport);
    141       gpr_ref(&connection_state->refs);
    142       GRPC_CLOSURE_INIT(&connection_state->on_receive_settings,
    143                         on_receive_settings, connection_state,
    144                         grpc_schedule_on_exec_ctx);
    145       grpc_chttp2_transport_start_reading(
    146           transport, args->read_buffer, &connection_state->on_receive_settings);
    147       grpc_channel_args_destroy(args->args);
    148       gpr_ref(&connection_state->refs);
    149       GRPC_CHTTP2_REF_TRANSPORT((grpc_chttp2_transport*)transport,
    150                                 "receive settings timeout");
    151       GRPC_CLOSURE_INIT(&connection_state->on_timeout, on_timeout,
    152                         connection_state, grpc_schedule_on_exec_ctx);
    153       grpc_timer_init(&connection_state->timer, connection_state->deadline,
    154                       &connection_state->on_timeout);
    155     }
    156   }
    157   grpc_handshake_manager_pending_list_remove(
    158       &connection_state->svr_state->pending_handshake_mgrs,
    159       connection_state->handshake_mgr);
    160   gpr_mu_unlock(&connection_state->svr_state->mu);
    161   grpc_handshake_manager_destroy(connection_state->handshake_mgr);
    162   gpr_free(connection_state->acceptor);
    163   grpc_tcp_server_unref(connection_state->svr_state->tcp_server);
    164   server_connection_state_unref(connection_state);
    165 }
    166 
    167 static void on_accept(void* arg, grpc_endpoint* tcp,
    168                       grpc_pollset* accepting_pollset,
    169                       grpc_tcp_server_acceptor* acceptor) {
    170   server_state* state = static_cast<server_state*>(arg);
    171   gpr_mu_lock(&state->mu);
    172   if (state->shutdown) {
    173     gpr_mu_unlock(&state->mu);
    174     grpc_endpoint_shutdown(tcp, GRPC_ERROR_NONE);
    175     grpc_endpoint_destroy(tcp);
    176     gpr_free(acceptor);
    177     return;
    178   }
    179   grpc_handshake_manager* handshake_mgr = grpc_handshake_manager_create();
    180   grpc_handshake_manager_pending_list_add(&state->pending_handshake_mgrs,
    181                                           handshake_mgr);
    182   grpc_tcp_server_ref(state->tcp_server);
    183   gpr_mu_unlock(&state->mu);
    184   server_connection_state* connection_state =
    185       static_cast<server_connection_state*>(
    186           gpr_zalloc(sizeof(*connection_state)));
    187   gpr_ref_init(&connection_state->refs, 1);
    188   connection_state->svr_state = state;
    189   connection_state->accepting_pollset = accepting_pollset;
    190   connection_state->acceptor = acceptor;
    191   connection_state->handshake_mgr = handshake_mgr;
    192   grpc_handshakers_add(HANDSHAKER_SERVER, state->args,
    193                        connection_state->handshake_mgr);
    194   const grpc_arg* timeout_arg =
    195       grpc_channel_args_find(state->args, GRPC_ARG_SERVER_HANDSHAKE_TIMEOUT_MS);
    196   connection_state->deadline =
    197       grpc_core::ExecCtx::Get()->Now() +
    198       grpc_channel_arg_get_integer(timeout_arg,
    199                                    {120 * GPR_MS_PER_SEC, 1, INT_MAX});
    200   grpc_handshake_manager_do_handshake(
    201       connection_state->handshake_mgr, nullptr /* interested_parties */, tcp,
    202       state->args, connection_state->deadline, acceptor, on_handshake_done,
    203       connection_state);
    204 }
    205 
    206 /* Server callback: start listening on our ports */
    207 static void server_start_listener(grpc_server* server, void* arg,
    208                                   grpc_pollset** pollsets,
    209                                   size_t pollset_count) {
    210   server_state* state = static_cast<server_state*>(arg);
    211   gpr_mu_lock(&state->mu);
    212   state->shutdown = false;
    213   gpr_mu_unlock(&state->mu);
    214   grpc_tcp_server_start(state->tcp_server, pollsets, pollset_count, on_accept,
    215                         state);
    216 }
    217 
    218 static void tcp_server_shutdown_complete(void* arg, grpc_error* error) {
    219   server_state* state = static_cast<server_state*>(arg);
    220   /* ensure all threads have unlocked */
    221   gpr_mu_lock(&state->mu);
    222   grpc_closure* destroy_done = state->server_destroy_listener_done;
    223   GPR_ASSERT(state->shutdown);
    224   grpc_handshake_manager_pending_list_shutdown_all(
    225       state->pending_handshake_mgrs, GRPC_ERROR_REF(error));
    226   gpr_mu_unlock(&state->mu);
    227   // Flush queued work before destroying handshaker factory, since that
    228   // may do a synchronous unref.
    229   grpc_core::ExecCtx::Get()->Flush();
    230   if (destroy_done != nullptr) {
    231     destroy_done->cb(destroy_done->cb_arg, GRPC_ERROR_REF(error));
    232     grpc_core::ExecCtx::Get()->Flush();
    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) */
    241 static void server_destroy_listener(grpc_server* server, void* arg,
    242                                     grpc_closure* destroy_done) {
    243   server_state* state = static_cast<server_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(tcp_server);
    250   grpc_tcp_server_unref(tcp_server);
    251 }
    252 
    253 grpc_error* grpc_chttp2_server_add_port(grpc_server* server, const char* addr,
    254                                         grpc_channel_args* args,
    255                                         int* port_num) {
    256   grpc_resolved_addresses* resolved = nullptr;
    257   grpc_tcp_server* tcp_server = nullptr;
    258   size_t i;
    259   size_t count = 0;
    260   int port_temp;
    261   grpc_error* err = GRPC_ERROR_NONE;
    262   server_state* state = nullptr;
    263   grpc_error** errors = nullptr;
    264   size_t naddrs = 0;
    265 
    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 = static_cast<server_state*>(gpr_zalloc(sizeof(*state)));
    274   GRPC_CLOSURE_INIT(&state->tcp_server_shutdown_complete,
    275                     tcp_server_shutdown_complete, state,
    276                     grpc_schedule_on_exec_ctx);
    277   err = grpc_tcp_server_create(&state->tcp_server_shutdown_complete, args,
    278                                &tcp_server);
    279   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->shutdown = true;
    287   gpr_mu_init(&state->mu);
    288 
    289   naddrs = resolved->naddrs;
    290   errors = static_cast<grpc_error**>(gpr_malloc(sizeof(*errors) * naddrs));
    291   for (i = 0; i < naddrs; i++) {
    292     errors[i] =
    293         grpc_tcp_server_add_port(tcp_server, &resolved->addrs[i], &port_temp);
    294     if (errors[i] == GRPC_ERROR_NONE) {
    295       if (*port_num == -1) {
    296         *port_num = port_temp;
    297       } else {
    298         GPR_ASSERT(*port_num == port_temp);
    299       }
    300       count++;
    301     }
    302   }
    303   if (count == 0) {
    304     char* msg;
    305     gpr_asprintf(&msg, "No address added out of total %" PRIuPTR " resolved",
    306                  naddrs);
    307     err = GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(msg, errors, naddrs);
    308     gpr_free(msg);
    309     goto error;
    310   } else if (count != naddrs) {
    311     char* msg;
    312     gpr_asprintf(&msg,
    313                  "Only %" PRIuPTR " addresses added out of total %" PRIuPTR
    314                  " resolved",
    315                  count, naddrs);
    316     err = GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(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 
    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(server, state, server_start_listener,
    328                            server_destroy_listener);
    329   goto done;
    330 
    331 /* Error path: cleanup and return */
    332 error:
    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(tcp_server);
    339   } else {
    340     grpc_channel_args_destroy(args);
    341     gpr_free(state);
    342   }
    343   *port_num = 0;
    344 
    345 done:
    346   if (errors != nullptr) {
    347     for (i = 0; i < naddrs; i++) {
    348       GRPC_ERROR_UNREF(errors[i]);
    349     }
    350     gpr_free(errors);
    351   }
    352   return err;
    353 }
    354