Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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/channel/connected_channel.h" |
| 35 | |
| 36 | #include <stdarg.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 40 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include "src/core/transport/transport.h" |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 42 | #include "src/core/profiling/timers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc/byte_buffer.h> |
| 44 | #include <grpc/support/alloc.h> |
| 45 | #include <grpc/support/log.h> |
| 46 | #include <grpc/support/slice_buffer.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
| 48 | #define MAX_BUFFER_LENGTH 8192 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | typedef struct connected_channel_channel_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | grpc_transport *transport; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | } channel_data; |
| 53 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | typedef struct connected_channel_call_data { void *unused; } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | |
| 56 | /* We perform a small hack to locate transport data alongside the connected |
| 57 | channel data in call allocations, to allow everything to be pulled in minimal |
| 58 | cache line requests */ |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 59 | #define TRANSPORT_STREAM_FROM_CALL_DATA(calld) ((grpc_stream *)((calld) + 1)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | #define CALL_DATA_FROM_TRANSPORT_STREAM(transport_stream) \ |
| 61 | (((call_data *)(transport_stream)) - 1) |
| 62 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | /* Intercept a call operation and either push it directly up or translate it |
| 64 | into transport stream operations */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | static void con_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 66 | grpc_call_element *elem, |
| 67 | grpc_transport_stream_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | call_data *calld = elem->call_data; |
| 69 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | grpc_transport_perform_stream_op(exec_ctx, chand->transport, |
| 73 | TRANSPORT_STREAM_FROM_CALL_DATA(calld), op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | static void con_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 77 | grpc_channel_element *elem, |
| 78 | grpc_transport_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | grpc_transport_perform_op(exec_ctx, chand->transport, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 85 | grpc_call_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | call_data *calld = elem->call_data; |
| 87 | channel_data *chand = elem->channel_data; |
| 88 | int r; |
| 89 | |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 90 | r = grpc_transport_init_stream( |
| 91 | exec_ctx, chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA(calld), |
| 92 | &args->call_stack->refcount, args->server_transport_data); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | GPR_ASSERT(r == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 96 | static void set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 97 | grpc_pollset *pollset) { |
| 98 | call_data *calld = elem->call_data; |
| 99 | channel_data *chand = elem->channel_data; |
| 100 | grpc_transport_set_pollset(exec_ctx, chand->transport, |
| 101 | TRANSPORT_STREAM_FROM_CALL_DATA(calld), pollset); |
| 102 | } |
| 103 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 104 | /* Destructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 106 | grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | call_data *calld = elem->call_data; |
| 108 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | grpc_transport_destroy_stream(exec_ctx, chand->transport, |
| 110 | TRANSPORT_STREAM_FROM_CALL_DATA(calld)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 115 | grpc_channel_element *elem, |
| 116 | grpc_channel_element_args *args) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | channel_data *cd = (channel_data *)elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 118 | GPR_ASSERT(args->is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 119 | cd->transport = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 124 | grpc_channel_element *elem) { |
| 125 | channel_data *cd = (channel_data *)elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | grpc_transport_destroy(exec_ctx, cd->transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 129 | static char *con_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 130 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | return grpc_transport_get_peer(exec_ctx, chand->transport); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 134 | static const grpc_channel_filter connected_channel_filter = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 135 | con_start_transport_stream_op, con_start_transport_op, sizeof(call_data), |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 136 | init_call_elem, set_pollset, destroy_call_elem, sizeof(channel_data), |
| 137 | init_channel_elem, destroy_channel_elem, con_get_peer, "connected", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 138 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 139 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 140 | static void bind_transport(grpc_channel_stack *channel_stack, |
| 141 | grpc_channel_element *elem, void *t) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 142 | channel_data *cd = (channel_data *)elem->channel_data; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 143 | GPR_ASSERT(elem->filter == &connected_channel_filter); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | GPR_ASSERT(cd->transport == NULL); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 145 | cd->transport = t; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | |
| 147 | /* HACK(ctiller): increase call stack size for the channel to make space |
| 148 | for channel data. We need a cleaner (but performant) way to do this, |
| 149 | and I'm not sure what that is yet. |
| 150 | This is only "safe" because call stacks place no additional data after |
| 151 | the last call element, and the last call element MUST be the connected |
| 152 | channel. */ |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 153 | channel_stack->call_stack_size += grpc_transport_stream_size(t); |
| 154 | } |
| 155 | |
| 156 | bool grpc_add_connected_filter(grpc_channel_stack_builder *builder, |
| 157 | void *arg_must_be_null) { |
| 158 | GPR_ASSERT(arg_must_be_null == NULL); |
| 159 | grpc_transport *t = grpc_channel_stack_builder_get_transport(builder); |
| 160 | GPR_ASSERT(t != NULL); |
| 161 | return grpc_channel_stack_builder_append_filter( |
| 162 | builder, &connected_channel_filter, bind_transport, t); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 163 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 164 | |
| 165 | grpc_stream *grpc_connected_channel_get_stream(grpc_call_element *elem) { |
| 166 | call_data *calld = elem->call_data; |
| 167 | return TRANSPORT_STREAM_FROM_CALL_DATA(calld); |
| 168 | } |