Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/channel/connected_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <stdarg.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/byte_buffer.h> |
| 41 | #include <grpc/support/alloc.h> |
| 42 | #include <grpc/support/log.h> |
| 43 | #include <grpc/support/slice_buffer.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 44 | #include "src/core/lib/profiling/timers.h" |
| 45 | #include "src/core/lib/support/string.h" |
| 46 | #include "src/core/lib/transport/transport.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 | |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 96 | static void set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, |
| 97 | grpc_call_element *elem, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 98 | grpc_polling_entity *pollent) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 99 | call_data *calld = elem->call_data; |
| 100 | channel_data *chand = elem->channel_data; |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 101 | grpc_transport_set_pops(exec_ctx, chand->transport, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 102 | TRANSPORT_STREAM_FROM_CALL_DATA(calld), pollent); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | /* Destructor for call_data */ |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 106 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 107 | const grpc_call_final_info *final_info, |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 108 | void *and_free_memory) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | call_data *calld = elem->call_data; |
| 110 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | grpc_transport_destroy_stream(exec_ctx, chand->transport, |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 112 | TRANSPORT_STREAM_FROM_CALL_DATA(calld), |
| 113 | and_free_memory); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 118 | grpc_channel_element *elem, |
| 119 | grpc_channel_element_args *args) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | channel_data *cd = (channel_data *)elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 121 | GPR_ASSERT(args->is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | cd->transport = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 127 | grpc_channel_element *elem) { |
| 128 | channel_data *cd = (channel_data *)elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 129 | grpc_transport_destroy(exec_ctx, cd->transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | 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] | 133 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | return grpc_transport_get_peer(exec_ctx, chand->transport); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 137 | static const grpc_channel_filter connected_channel_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 138 | con_start_transport_stream_op, |
| 139 | con_start_transport_op, |
| 140 | sizeof(call_data), |
| 141 | init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 142 | set_pollset_or_pollset_set, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 143 | destroy_call_elem, |
| 144 | sizeof(channel_data), |
| 145 | init_channel_elem, |
| 146 | destroy_channel_elem, |
| 147 | con_get_peer, |
| 148 | "connected", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 149 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 151 | static void bind_transport(grpc_channel_stack *channel_stack, |
| 152 | grpc_channel_element *elem, void *t) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | channel_data *cd = (channel_data *)elem->channel_data; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 154 | GPR_ASSERT(elem->filter == &connected_channel_filter); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | GPR_ASSERT(cd->transport == NULL); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 156 | cd->transport = t; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | |
| 158 | /* HACK(ctiller): increase call stack size for the channel to make space |
| 159 | for channel data. We need a cleaner (but performant) way to do this, |
| 160 | and I'm not sure what that is yet. |
| 161 | This is only "safe" because call stacks place no additional data after |
| 162 | the last call element, and the last call element MUST be the connected |
| 163 | channel. */ |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 164 | channel_stack->call_stack_size += grpc_transport_stream_size(t); |
| 165 | } |
| 166 | |
| 167 | bool grpc_add_connected_filter(grpc_channel_stack_builder *builder, |
| 168 | void *arg_must_be_null) { |
| 169 | GPR_ASSERT(arg_must_be_null == NULL); |
| 170 | grpc_transport *t = grpc_channel_stack_builder_get_transport(builder); |
| 171 | GPR_ASSERT(t != NULL); |
| 172 | return grpc_channel_stack_builder_append_filter( |
| 173 | builder, &connected_channel_filter, bind_transport, t); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 174 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 175 | |
| 176 | grpc_stream *grpc_connected_channel_get_stream(grpc_call_element *elem) { |
| 177 | call_data *calld = elem->call_data; |
| 178 | return TRANSPORT_STREAM_FROM_CALL_DATA(calld); |
| 179 | } |