OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 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 #ifndef GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H |
| 35 #define GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H |
| 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> |
| 48 #include "src/core/debug/trace.h" |
| 49 #include "src/core/transport/transport.h" |
| 50 |
| 51 typedef struct grpc_channel_element grpc_channel_element; |
| 52 typedef struct grpc_call_element grpc_call_element; |
| 53 |
| 54 typedef struct grpc_channel_stack grpc_channel_stack; |
| 55 typedef struct grpc_call_stack grpc_call_stack; |
| 56 |
| 57 typedef struct { |
| 58 grpc_channel_stack *channel_stack; |
| 59 const grpc_channel_args *channel_args; |
| 60 int is_first; |
| 61 int is_last; |
| 62 } grpc_channel_element_args; |
| 63 |
| 64 typedef struct { |
| 65 grpc_call_stack *call_stack; |
| 66 const void *server_transport_data; |
| 67 grpc_call_context_element *context; |
| 68 } grpc_call_element_args; |
| 69 |
| 70 /* Channel filters specify: |
| 71 1. the amount of memory needed in the channel & call (via the sizeof_XXX |
| 72 members) |
| 73 2. functions to initialize and destroy channel & call data |
| 74 (init_XXX, destroy_XXX) |
| 75 3. functions to implement call operations and channel operations (call_op, |
| 76 channel_op) |
| 77 4. a name, which is useful when debugging |
| 78 |
| 79 Members are laid out in approximate frequency of use order. */ |
| 80 typedef struct { |
| 81 /* Called to eg. send/receive data on a call. |
| 82 See grpc_call_next_op on how to call the next element in the stack */ |
| 83 void (*start_transport_stream_op)(grpc_exec_ctx *exec_ctx, |
| 84 grpc_call_element *elem, |
| 85 grpc_transport_stream_op *op); |
| 86 /* Called to handle channel level operations - e.g. new calls, or transport |
| 87 closure. |
| 88 See grpc_channel_next_op on how to call the next element in the stack */ |
| 89 void (*start_transport_op)(grpc_exec_ctx *exec_ctx, |
| 90 grpc_channel_element *elem, grpc_transport_op *op); |
| 91 |
| 92 /* sizeof(per call data) */ |
| 93 size_t sizeof_call_data; |
| 94 /* Initialize per call data. |
| 95 elem is initialized at the start of the call, and elem->call_data is what |
| 96 needs initializing. |
| 97 The filter does not need to do any chaining. |
| 98 server_transport_data is an opaque pointer. If it is NULL, this call is |
| 99 on a client; if it is non-NULL, then it points to memory owned by the |
| 100 transport and is on the server. Most filters want to ignore this |
| 101 argument. */ |
| 102 void (*init_call_elem)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 103 grpc_call_element_args *args); |
| 104 void (*set_pollset)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 105 grpc_pollset *pollset); |
| 106 /* Destroy per call data. |
| 107 The filter does not need to do any chaining */ |
| 108 void (*destroy_call_elem)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem); |
| 109 |
| 110 /* sizeof(per channel data) */ |
| 111 size_t sizeof_channel_data; |
| 112 /* Initialize per-channel data. |
| 113 elem is initialized at the start of the call, and elem->channel_data is |
| 114 what needs initializing. |
| 115 is_first, is_last designate this elements position in the stack, and are |
| 116 useful for asserting correct configuration by upper layer code. |
| 117 The filter does not need to do any chaining */ |
| 118 void (*init_channel_elem)(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 119 grpc_channel_element_args *args); |
| 120 /* Destroy per channel data. |
| 121 The filter does not need to do any chaining */ |
| 122 void (*destroy_channel_elem)(grpc_exec_ctx *exec_ctx, |
| 123 grpc_channel_element *elem); |
| 124 |
| 125 /* Implement grpc_call_get_peer() */ |
| 126 char *(*get_peer)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem); |
| 127 |
| 128 /* The name of this filter */ |
| 129 const char *name; |
| 130 } grpc_channel_filter; |
| 131 |
| 132 /* A channel_element tracks its filter and the filter requested memory within |
| 133 a channel allocation */ |
| 134 struct grpc_channel_element { |
| 135 const grpc_channel_filter *filter; |
| 136 void *channel_data; |
| 137 }; |
| 138 |
| 139 /* A call_element tracks its filter, the filter requested memory within |
| 140 a channel allocation, and the filter requested memory within a call |
| 141 allocation */ |
| 142 struct grpc_call_element { |
| 143 const grpc_channel_filter *filter; |
| 144 void *channel_data; |
| 145 void *call_data; |
| 146 }; |
| 147 |
| 148 /* A channel stack tracks a set of related filters for one channel, and |
| 149 guarantees they live within a single malloc() allocation */ |
| 150 struct grpc_channel_stack { |
| 151 grpc_stream_refcount refcount; |
| 152 size_t count; |
| 153 /* Memory required for a call stack (computed at channel stack |
| 154 initialization) */ |
| 155 size_t call_stack_size; |
| 156 }; |
| 157 |
| 158 /* A call stack tracks a set of related filters for one call, and guarantees |
| 159 they live within a single malloc() allocation */ |
| 160 struct grpc_call_stack { |
| 161 /* shared refcount for this channel stack. |
| 162 MUST be the first element: the underlying code calls destroy |
| 163 with the address of the refcount, but higher layers prefer to think |
| 164 about the address of the call stack itself. */ |
| 165 grpc_stream_refcount refcount; |
| 166 size_t count; |
| 167 }; |
| 168 |
| 169 /* Get a channel element given a channel stack and its index */ |
| 170 grpc_channel_element *grpc_channel_stack_element(grpc_channel_stack *stack, |
| 171 size_t i); |
| 172 /* Get the last channel element in a channel stack */ |
| 173 grpc_channel_element *grpc_channel_stack_last_element( |
| 174 grpc_channel_stack *stack); |
| 175 /* Get a call stack element given a call stack and an index */ |
| 176 grpc_call_element *grpc_call_stack_element(grpc_call_stack *stack, size_t i); |
| 177 |
| 178 /* Determine memory required for a channel stack containing a set of filters */ |
| 179 size_t grpc_channel_stack_size(const grpc_channel_filter **filters, |
| 180 size_t filter_count); |
| 181 /* Initialize a channel stack given some filters */ |
| 182 void grpc_channel_stack_init(grpc_exec_ctx *exec_ctx, int initial_refs, |
| 183 grpc_iomgr_cb_func destroy, void *destroy_arg, |
| 184 const grpc_channel_filter **filters, |
| 185 size_t filter_count, const grpc_channel_args *args, |
| 186 const char *name, grpc_channel_stack *stack); |
| 187 /* Destroy a channel stack */ |
| 188 void grpc_channel_stack_destroy(grpc_exec_ctx *exec_ctx, |
| 189 grpc_channel_stack *stack); |
| 190 |
| 191 /* Initialize a call stack given a channel stack. transport_server_data is |
| 192 expected to be NULL on a client, or an opaque transport owned pointer on the |
| 193 server. */ |
| 194 void grpc_call_stack_init(grpc_exec_ctx *exec_ctx, |
| 195 grpc_channel_stack *channel_stack, int initial_refs, |
| 196 grpc_iomgr_cb_func destroy, void *destroy_arg, |
| 197 grpc_call_context_element *context, |
| 198 const void *transport_server_data, |
| 199 grpc_call_stack *call_stack); |
| 200 /* Set a pollset for a call stack: must occur before the first op is started */ |
| 201 void grpc_call_stack_set_pollset(grpc_exec_ctx *exec_ctx, |
| 202 grpc_call_stack *call_stack, |
| 203 grpc_pollset *pollset); |
| 204 |
| 205 #ifdef GRPC_STREAM_REFCOUNT_DEBUG |
| 206 #define GRPC_CALL_STACK_REF(call_stack, reason) \ |
| 207 grpc_stream_ref(&(call_stack)->refcount, reason) |
| 208 #define GRPC_CALL_STACK_UNREF(exec_ctx, call_stack, reason) \ |
| 209 grpc_stream_unref(exec_ctx, &(call_stack)->refcount, reason) |
| 210 #define GRPC_CHANNEL_STACK_REF(channel_stack, reason) \ |
| 211 grpc_stream_ref(&(channel_stack)->refcount, reason) |
| 212 #define GRPC_CHANNEL_STACK_UNREF(exec_ctx, channel_stack, reason) \ |
| 213 grpc_stream_unref(exec_ctx, &(channel_stack)->refcount, reason) |
| 214 #else |
| 215 #define GRPC_CALL_STACK_REF(call_stack, reason) \ |
| 216 grpc_stream_ref(&(call_stack)->refcount) |
| 217 #define GRPC_CALL_STACK_UNREF(exec_ctx, call_stack, reason) \ |
| 218 grpc_stream_unref(exec_ctx, &(call_stack)->refcount) |
| 219 #define GRPC_CHANNEL_STACK_REF(channel_stack, reason) \ |
| 220 grpc_stream_ref(&(channel_stack)->refcount) |
| 221 #define GRPC_CHANNEL_STACK_UNREF(exec_ctx, channel_stack, reason) \ |
| 222 grpc_stream_unref(exec_ctx, &(channel_stack)->refcount) |
| 223 #endif |
| 224 |
| 225 /* Destroy a call stack */ |
| 226 void grpc_call_stack_destroy(grpc_exec_ctx *exec_ctx, grpc_call_stack *stack); |
| 227 |
| 228 /* Ignore set pollset - used by filters to implement the set_pollset method |
| 229 if they don't care about pollsets at all. Does nothing. */ |
| 230 void grpc_call_stack_ignore_set_pollset(grpc_exec_ctx *exec_ctx, |
| 231 grpc_call_element *elem, |
| 232 grpc_pollset *pollset); |
| 233 /* Call the next operation in a call stack */ |
| 234 void grpc_call_next_op(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 235 grpc_transport_stream_op *op); |
| 236 /* Call the next operation (depending on call directionality) in a channel |
| 237 stack */ |
| 238 void grpc_channel_next_op(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 239 grpc_transport_op *op); |
| 240 /* Pass through a request to get_peer to the next child element */ |
| 241 char *grpc_call_next_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem); |
| 242 |
| 243 /* Given the top element of a channel stack, get the channel stack itself */ |
| 244 grpc_channel_stack *grpc_channel_stack_from_top_element( |
| 245 grpc_channel_element *elem); |
| 246 /* Given the top element of a call stack, get the call stack itself */ |
| 247 grpc_call_stack *grpc_call_stack_from_top_element(grpc_call_element *elem); |
| 248 |
| 249 void grpc_call_log_op(char *file, int line, gpr_log_severity severity, |
| 250 grpc_call_element *elem, grpc_transport_stream_op *op); |
| 251 |
| 252 void grpc_call_element_send_cancel(grpc_exec_ctx *exec_ctx, |
| 253 grpc_call_element *cur_elem); |
| 254 |
| 255 extern int grpc_trace_channel; |
| 256 |
| 257 #define GRPC_CALL_LOG_OP(sev, elem, op) \ |
| 258 if (grpc_trace_channel) grpc_call_log_op(sev, elem, op) |
| 259 |
| 260 #endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H */ |
OLD | NEW |