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" |
| 42 | #include <grpc/byte_buffer.h> |
| 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
| 45 | #include <grpc/support/slice_buffer.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
| 47 | #define MAX_BUFFER_LENGTH 8192 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
Craig Tiller | f235018 | 2015-03-02 16:39:39 +0000 | [diff] [blame] | 49 | typedef struct connected_channel_channel_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | grpc_transport *transport; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | } channel_data; |
| 52 | |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 53 | typedef struct connected_channel_call_data { void *unused; } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | |
| 55 | /* We perform a small hack to locate transport data alongside the connected |
| 56 | channel data in call allocations, to allow everything to be pulled in minimal |
| 57 | cache line requests */ |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 58 | #define TRANSPORT_STREAM_FROM_CALL_DATA(calld) ((grpc_stream *)((calld) + 1)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | #define CALL_DATA_FROM_TRANSPORT_STREAM(transport_stream) \ |
| 60 | (((call_data *)(transport_stream)) - 1) |
| 61 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | /* Intercept a call operation and either push it directly up or translate it |
| 63 | into transport stream operations */ |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 64 | static void con_start_transport_stream_op(grpc_call_element *elem, |
| 65 | grpc_transport_stream_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | call_data *calld = elem->call_data; |
| 67 | channel_data *chand = elem->channel_data; |
| 68 | GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 69 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 70 | |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 71 | grpc_transport_perform_stream_op(chand->transport, |
| 72 | TRANSPORT_STREAM_FROM_CALL_DATA(calld), op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 75 | static void con_start_transport_op(grpc_channel_element *elem, |
| 76 | grpc_transport_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | channel_data *chand = elem->channel_data; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 78 | grpc_transport_perform_op(chand->transport, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* Constructor for call_data */ |
| 82 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 83 | const void *server_transport_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 84 | grpc_transport_stream_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | call_data *calld = elem->call_data; |
| 86 | channel_data *chand = elem->channel_data; |
| 87 | int r; |
| 88 | |
| 89 | GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 90 | r = grpc_transport_init_stream(chand->transport, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | TRANSPORT_STREAM_FROM_CALL_DATA(calld), |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 92 | server_transport_data, initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | GPR_ASSERT(r == 0); |
| 94 | } |
| 95 | |
| 96 | /* Destructor for call_data */ |
| 97 | static void destroy_call_elem(grpc_call_element *elem) { |
| 98 | call_data *calld = elem->call_data; |
| 99 | channel_data *chand = elem->channel_data; |
| 100 | GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | grpc_transport_destroy_stream(chand->transport, |
| 102 | TRANSPORT_STREAM_FROM_CALL_DATA(calld)); |
| 103 | } |
| 104 | |
| 105 | /* Constructor for channel_data */ |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 106 | static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 108 | int is_first, int is_last) { |
| 109 | channel_data *cd = (channel_data *)elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | GPR_ASSERT(is_last); |
| 111 | GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 112 | cd->transport = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Destructor for channel_data */ |
| 116 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 117 | channel_data *cd = (channel_data *)elem->channel_data; |
| 118 | GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 119 | grpc_transport_destroy(cd->transport); |
| 120 | } |
| 121 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 122 | static char *con_get_peer(grpc_call_element *elem) { |
| 123 | channel_data *chand = elem->channel_data; |
| 124 | return grpc_transport_get_peer(chand->transport); |
| 125 | } |
| 126 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | const grpc_channel_filter grpc_connected_channel_filter = { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 128 | con_start_transport_stream_op, |
| 129 | con_start_transport_op, |
| 130 | sizeof(call_data), |
| 131 | init_call_elem, |
| 132 | destroy_call_elem, |
| 133 | sizeof(channel_data), |
| 134 | init_channel_elem, |
| 135 | destroy_channel_elem, |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 136 | con_get_peer, |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 137 | "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 | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 140 | void grpc_connected_channel_bind_transport(grpc_channel_stack *channel_stack, |
| 141 | grpc_transport *transport) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | /* Assumes that the connected channel filter is always the last filter |
| 143 | in a channel stack */ |
| 144 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 145 | channel_data *cd = (channel_data *)elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 147 | GPR_ASSERT(cd->transport == NULL); |
| 148 | cd->transport = transport; |
| 149 | |
| 150 | /* HACK(ctiller): increase call stack size for the channel to make space |
| 151 | for channel data. We need a cleaner (but performant) way to do this, |
| 152 | and I'm not sure what that is yet. |
| 153 | This is only "safe" because call stacks place no additional data after |
| 154 | the last call element, and the last call element MUST be the connected |
| 155 | channel. */ |
| 156 | channel_stack->call_stack_size += grpc_transport_stream_size(transport); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 157 | } |