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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H |
| 35 | #define GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | /* A channel filter defines how operations on a channel are implemented. |
| 38 | Channel filters are chained together to create full channels, and if those |
| 39 | chains are linear, then channel stacks provide a mechanism to minimize |
| 40 | allocations for that chain. |
| 41 | Call stacks are created by channel stacks and represent the per-call data |
| 42 | for that stack. */ |
| 43 | |
| 44 | #include <stddef.h> |
| 45 | |
| 46 | #include <grpc/grpc.h> |
| 47 | #include <grpc/support/log.h> |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 48 | #include "src/core/debug/trace.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include "src/core/transport/transport.h" |
| 50 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | typedef struct grpc_channel_element grpc_channel_element; |
| 52 | typedef struct grpc_call_element grpc_call_element; |
| 53 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | /* The direction of the call. |
| 55 | The values of the enums (1, -1) matter here - they are used to increment |
| 56 | or decrement a pointer to find the next element to call */ |
| 57 | typedef enum { GRPC_CALL_DOWN = 1, GRPC_CALL_UP = -1 } grpc_call_dir; |
| 58 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | typedef enum { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 60 | /* send a goaway message to remote channels indicating that we are going |
| 61 | to disconnect in the future */ |
| 62 | GRPC_CHANNEL_GOAWAY, |
| 63 | /* disconnect any underlying transports */ |
| 64 | GRPC_CHANNEL_DISCONNECT, |
| 65 | /* transport received a new call */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | GRPC_ACCEPT_CALL, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 67 | /* an underlying transport was closed */ |
| 68 | GRPC_TRANSPORT_CLOSED, |
| 69 | /* an underlying transport is about to be closed */ |
| 70 | GRPC_TRANSPORT_GOAWAY |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | } grpc_channel_op_type; |
| 72 | |
| 73 | /* A single filterable operation to be performed on a channel */ |
| 74 | typedef struct { |
| 75 | /* The type of operation we're performing */ |
| 76 | grpc_channel_op_type type; |
| 77 | /* The directionality of this call - is it bubbling up the stack, or down? */ |
| 78 | grpc_call_dir dir; |
| 79 | |
| 80 | /* Argument data, matching up with grpc_channel_op_type names */ |
| 81 | union { |
| 82 | struct { |
| 83 | grpc_transport *transport; |
| 84 | const void *transport_server_data; |
| 85 | } accept_call; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 86 | struct { |
| 87 | grpc_status_code status; |
| 88 | gpr_slice message; |
| 89 | } goaway; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } data; |
| 91 | } grpc_channel_op; |
| 92 | |
| 93 | /* Channel filters specify: |
| 94 | 1. the amount of memory needed in the channel & call (via the sizeof_XXX |
| 95 | members) |
| 96 | 2. functions to initialize and destroy channel & call data |
| 97 | (init_XXX, destroy_XXX) |
| 98 | 3. functions to implement call operations and channel operations (call_op, |
| 99 | channel_op) |
| 100 | 4. a name, which is useful when debugging |
| 101 | |
| 102 | Members are laid out in approximate frequency of use order. */ |
| 103 | typedef struct { |
| 104 | /* Called to eg. send/receive data on a call. |
| 105 | See grpc_call_next_op on how to call the next element in the stack */ |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame^] | 106 | void (*start_transport_op)(grpc_call_element *elem, |
| 107 | grpc_transport_stream_op *op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | /* Called to handle channel level operations - e.g. new calls, or transport |
| 109 | closure. |
| 110 | See grpc_channel_next_op on how to call the next element in the stack */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 111 | void (*channel_op)(grpc_channel_element *elem, |
| 112 | grpc_channel_element *from_elem, grpc_channel_op *op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | |
| 114 | /* sizeof(per call data) */ |
| 115 | size_t sizeof_call_data; |
| 116 | /* Initialize per call data. |
| 117 | elem is initialized at the start of the call, and elem->call_data is what |
| 118 | needs initializing. |
| 119 | The filter does not need to do any chaining. |
| 120 | server_transport_data is an opaque pointer. If it is NULL, this call is |
| 121 | on a client; if it is non-NULL, then it points to memory owned by the |
| 122 | transport and is on the server. Most filters want to ignore this |
| 123 | argument.*/ |
| 124 | void (*init_call_elem)(grpc_call_element *elem, |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 125 | const void *server_transport_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame^] | 126 | grpc_transport_stream_op *initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | /* Destroy per call data. |
| 128 | The filter does not need to do any chaining */ |
| 129 | void (*destroy_call_elem)(grpc_call_element *elem); |
| 130 | |
| 131 | /* sizeof(per channel data) */ |
| 132 | size_t sizeof_channel_data; |
| 133 | /* Initialize per-channel data. |
| 134 | elem is initialized at the start of the call, and elem->channel_data is |
| 135 | what needs initializing. |
| 136 | is_first, is_last designate this elements position in the stack, and are |
| 137 | useful for asserting correct configuration by upper layer code. |
| 138 | The filter does not need to do any chaining */ |
| 139 | void (*init_channel_elem)(grpc_channel_element *elem, |
| 140 | const grpc_channel_args *args, |
| 141 | grpc_mdctx *metadata_context, int is_first, |
| 142 | int is_last); |
| 143 | /* Destroy per channel data. |
| 144 | The filter does not need to do any chaining */ |
| 145 | void (*destroy_channel_elem)(grpc_channel_element *elem); |
| 146 | |
| 147 | /* The name of this filter */ |
| 148 | const char *name; |
| 149 | } grpc_channel_filter; |
| 150 | |
| 151 | /* A channel_element tracks its filter and the filter requested memory within |
| 152 | a channel allocation */ |
| 153 | struct grpc_channel_element { |
| 154 | const grpc_channel_filter *filter; |
| 155 | void *channel_data; |
| 156 | }; |
| 157 | |
| 158 | /* A call_element tracks its filter, the filter requested memory within |
| 159 | a channel allocation, and the filter requested memory within a call |
| 160 | allocation */ |
| 161 | struct grpc_call_element { |
| 162 | const grpc_channel_filter *filter; |
| 163 | void *channel_data; |
| 164 | void *call_data; |
| 165 | }; |
| 166 | |
| 167 | /* A channel stack tracks a set of related filters for one channel, and |
| 168 | guarantees they live within a single malloc() allocation */ |
| 169 | typedef struct { |
| 170 | size_t count; |
| 171 | /* Memory required for a call stack (computed at channel stack |
| 172 | initialization) */ |
| 173 | size_t call_stack_size; |
| 174 | } grpc_channel_stack; |
| 175 | |
| 176 | /* A call stack tracks a set of related filters for one call, and guarantees |
| 177 | they live within a single malloc() allocation */ |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 178 | typedef struct { size_t count; } grpc_call_stack; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 179 | |
| 180 | /* Get a channel element given a channel stack and its index */ |
| 181 | grpc_channel_element *grpc_channel_stack_element(grpc_channel_stack *stack, |
| 182 | size_t i); |
| 183 | /* Get the last channel element in a channel stack */ |
| 184 | grpc_channel_element *grpc_channel_stack_last_element( |
| 185 | grpc_channel_stack *stack); |
| 186 | /* Get a call stack element given a call stack and an index */ |
| 187 | grpc_call_element *grpc_call_stack_element(grpc_call_stack *stack, size_t i); |
| 188 | |
| 189 | /* Determine memory required for a channel stack containing a set of filters */ |
| 190 | size_t grpc_channel_stack_size(const grpc_channel_filter **filters, |
| 191 | size_t filter_count); |
| 192 | /* Initialize a channel stack given some filters */ |
| 193 | void grpc_channel_stack_init(const grpc_channel_filter **filters, |
| 194 | size_t filter_count, const grpc_channel_args *args, |
| 195 | grpc_mdctx *metadata_context, |
| 196 | grpc_channel_stack *stack); |
| 197 | /* Destroy a channel stack */ |
| 198 | void grpc_channel_stack_destroy(grpc_channel_stack *stack); |
| 199 | |
| 200 | /* Initialize a call stack given a channel stack. transport_server_data is |
| 201 | expected to be NULL on a client, or an opaque transport owned pointer on the |
| 202 | server. */ |
| 203 | void grpc_call_stack_init(grpc_channel_stack *channel_stack, |
| 204 | const void *transport_server_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame^] | 205 | grpc_transport_stream_op *initial_op, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | grpc_call_stack *call_stack); |
| 207 | /* Destroy a call stack */ |
| 208 | void grpc_call_stack_destroy(grpc_call_stack *stack); |
| 209 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 210 | /* Call the next operation in a call stack */ |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame^] | 211 | void grpc_call_next_op(grpc_call_element *elem, grpc_transport_stream_op *op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | /* Call the next operation (depending on call directionality) in a channel |
| 213 | stack */ |
| 214 | void grpc_channel_next_op(grpc_channel_element *elem, grpc_channel_op *op); |
| 215 | |
| 216 | /* Given the top element of a channel stack, get the channel stack itself */ |
| 217 | grpc_channel_stack *grpc_channel_stack_from_top_element( |
| 218 | grpc_channel_element *elem); |
| 219 | /* Given the top element of a call stack, get the call stack itself */ |
| 220 | grpc_call_stack *grpc_call_stack_from_top_element(grpc_call_element *elem); |
| 221 | |
| 222 | void grpc_call_log_op(char *file, int line, gpr_log_severity severity, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame^] | 223 | grpc_call_element *elem, grpc_transport_stream_op *op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 225 | void grpc_call_element_send_cancel(grpc_call_element *cur_elem); |
| 226 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 227 | extern int grpc_trace_channel; |
| 228 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 229 | #define GRPC_CALL_LOG_OP(sev, elem, op) \ |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 230 | if (grpc_trace_channel) grpc_call_log_op(sev, elem, op) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 231 | |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 232 | #endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H */ |