Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 9b52412 | 2016-02-18 08:23:08 -0800 | [diff] [blame] | 3 | * Copyright 2016, Google Inc. |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -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/channel_stack_builder.h" |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 39 | #include <grpc/support/string_util.h> |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 40 | |
Craig Tiller | de67626 | 2016-02-19 12:28:34 -0800 | [diff] [blame] | 41 | int grpc_trace_channel_stack_builder = 0; |
| 42 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 43 | typedef struct filter_node { |
| 44 | struct filter_node *next; |
| 45 | struct filter_node *prev; |
| 46 | const grpc_channel_filter *filter; |
| 47 | grpc_post_filter_create_init_func init; |
| 48 | void *init_arg; |
| 49 | } filter_node; |
| 50 | |
| 51 | struct grpc_channel_stack_builder { |
Craig Tiller | 6fcb64c | 2016-02-22 16:35:31 -0800 | [diff] [blame] | 52 | // sentinel nodes for filters that have been added |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 53 | filter_node begin; |
| 54 | filter_node end; |
| 55 | // various set/get-able parameters |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 56 | grpc_channel_args *args; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 57 | grpc_transport *transport; |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 58 | char *target; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 59 | const char *name; |
| 60 | }; |
| 61 | |
| 62 | struct grpc_channel_stack_builder_iterator { |
| 63 | grpc_channel_stack_builder *builder; |
| 64 | filter_node *node; |
| 65 | }; |
| 66 | |
| 67 | grpc_channel_stack_builder *grpc_channel_stack_builder_create(void) { |
| 68 | grpc_channel_stack_builder *b = gpr_malloc(sizeof(*b)); |
| 69 | memset(b, 0, sizeof(*b)); |
| 70 | |
Craig Tiller | 6fcb64c | 2016-02-22 16:35:31 -0800 | [diff] [blame] | 71 | b->begin.filter = NULL; |
| 72 | b->end.filter = NULL; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 73 | b->begin.next = &b->end; |
| 74 | b->begin.prev = &b->end; |
| 75 | b->end.next = &b->begin; |
| 76 | b->end.prev = &b->begin; |
| 77 | |
| 78 | return b; |
| 79 | } |
| 80 | |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 81 | void grpc_channel_stack_builder_set_target(grpc_channel_stack_builder *b, |
| 82 | const char *target) { |
| 83 | gpr_free(b->target); |
| 84 | b->target = gpr_strdup(target); |
| 85 | } |
| 86 | |
Craig Tiller | 0b54163 | 2016-04-05 17:21:05 -0700 | [diff] [blame] | 87 | const char *grpc_channel_stack_builder_get_target( |
| 88 | grpc_channel_stack_builder *b) { |
| 89 | return b->target; |
| 90 | } |
| 91 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 92 | static grpc_channel_stack_builder_iterator *create_iterator_at_filter_node( |
| 93 | grpc_channel_stack_builder *builder, filter_node *node) { |
| 94 | grpc_channel_stack_builder_iterator *it = gpr_malloc(sizeof(*it)); |
| 95 | it->builder = builder; |
| 96 | it->node = node; |
| 97 | return it; |
| 98 | } |
| 99 | |
| 100 | void grpc_channel_stack_builder_iterator_destroy( |
| 101 | grpc_channel_stack_builder_iterator *it) { |
| 102 | gpr_free(it); |
| 103 | } |
| 104 | |
| 105 | grpc_channel_stack_builder_iterator * |
| 106 | grpc_channel_stack_builder_create_iterator_at_first( |
| 107 | grpc_channel_stack_builder *builder) { |
| 108 | return create_iterator_at_filter_node(builder, &builder->begin); |
| 109 | } |
| 110 | |
| 111 | grpc_channel_stack_builder_iterator * |
| 112 | grpc_channel_stack_builder_create_iterator_at_last( |
| 113 | grpc_channel_stack_builder *builder) { |
| 114 | return create_iterator_at_filter_node(builder, &builder->end); |
| 115 | } |
| 116 | |
| 117 | bool grpc_channel_stack_builder_move_next( |
| 118 | grpc_channel_stack_builder_iterator *iterator) { |
| 119 | if (iterator->node == &iterator->builder->end) return false; |
| 120 | iterator->node = iterator->node->next; |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | bool grpc_channel_stack_builder_move_prev( |
| 125 | grpc_channel_stack_builder_iterator *iterator) { |
| 126 | if (iterator->node == &iterator->builder->begin) return false; |
| 127 | iterator->node = iterator->node->prev; |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool grpc_channel_stack_builder_move_prev( |
| 132 | grpc_channel_stack_builder_iterator *iterator); |
| 133 | |
| 134 | void grpc_channel_stack_builder_set_name(grpc_channel_stack_builder *builder, |
| 135 | const char *name) { |
| 136 | GPR_ASSERT(builder->name == NULL); |
| 137 | builder->name = name; |
| 138 | } |
| 139 | |
| 140 | void grpc_channel_stack_builder_set_channel_arguments( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 141 | grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder, |
| 142 | const grpc_channel_args *args) { |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 143 | if (builder->args != NULL) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 144 | grpc_channel_args_destroy(exec_ctx, builder->args); |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 145 | } |
Craig Tiller | 16c1345 | 2016-04-05 20:17:38 -0700 | [diff] [blame] | 146 | builder->args = grpc_channel_args_copy(args); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void grpc_channel_stack_builder_set_transport( |
| 150 | grpc_channel_stack_builder *builder, grpc_transport *transport) { |
| 151 | GPR_ASSERT(builder->transport == NULL); |
| 152 | builder->transport = transport; |
| 153 | } |
| 154 | |
| 155 | grpc_transport *grpc_channel_stack_builder_get_transport( |
| 156 | grpc_channel_stack_builder *builder) { |
| 157 | return builder->transport; |
| 158 | } |
| 159 | |
| 160 | const grpc_channel_args *grpc_channel_stack_builder_get_channel_arguments( |
| 161 | grpc_channel_stack_builder *builder) { |
| 162 | return builder->args; |
| 163 | } |
| 164 | |
| 165 | bool grpc_channel_stack_builder_append_filter( |
| 166 | grpc_channel_stack_builder *builder, const grpc_channel_filter *filter, |
| 167 | grpc_post_filter_create_init_func post_init_func, void *user_data) { |
| 168 | grpc_channel_stack_builder_iterator *it = |
| 169 | grpc_channel_stack_builder_create_iterator_at_last(builder); |
| 170 | bool ok = grpc_channel_stack_builder_add_filter_before( |
| 171 | it, filter, post_init_func, user_data); |
| 172 | grpc_channel_stack_builder_iterator_destroy(it); |
| 173 | return ok; |
| 174 | } |
| 175 | |
| 176 | bool grpc_channel_stack_builder_prepend_filter( |
| 177 | grpc_channel_stack_builder *builder, const grpc_channel_filter *filter, |
| 178 | grpc_post_filter_create_init_func post_init_func, void *user_data) { |
| 179 | grpc_channel_stack_builder_iterator *it = |
| 180 | grpc_channel_stack_builder_create_iterator_at_first(builder); |
| 181 | bool ok = grpc_channel_stack_builder_add_filter_after( |
| 182 | it, filter, post_init_func, user_data); |
| 183 | grpc_channel_stack_builder_iterator_destroy(it); |
| 184 | return ok; |
| 185 | } |
| 186 | |
| 187 | static void add_after(filter_node *before, const grpc_channel_filter *filter, |
| 188 | grpc_post_filter_create_init_func post_init_func, |
| 189 | void *user_data) { |
| 190 | filter_node *new = gpr_malloc(sizeof(*new)); |
| 191 | new->next = before->next; |
| 192 | new->prev = before; |
| 193 | new->next->prev = new->prev->next = new; |
| 194 | new->filter = filter; |
| 195 | new->init = post_init_func; |
| 196 | new->init_arg = user_data; |
| 197 | } |
| 198 | |
| 199 | bool grpc_channel_stack_builder_add_filter_before( |
| 200 | grpc_channel_stack_builder_iterator *iterator, |
| 201 | const grpc_channel_filter *filter, |
| 202 | grpc_post_filter_create_init_func post_init_func, void *user_data) { |
| 203 | if (iterator->node == &iterator->builder->begin) return false; |
| 204 | add_after(iterator->node->prev, filter, post_init_func, user_data); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | bool grpc_channel_stack_builder_add_filter_after( |
| 209 | grpc_channel_stack_builder_iterator *iterator, |
| 210 | const grpc_channel_filter *filter, |
| 211 | grpc_post_filter_create_init_func post_init_func, void *user_data) { |
| 212 | if (iterator->node == &iterator->builder->end) return false; |
| 213 | add_after(iterator->node, filter, post_init_func, user_data); |
| 214 | return true; |
| 215 | } |
| 216 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 217 | void grpc_channel_stack_builder_destroy(grpc_exec_ctx *exec_ctx, |
| 218 | grpc_channel_stack_builder *builder) { |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 219 | filter_node *p = builder->begin.next; |
| 220 | while (p != &builder->end) { |
| 221 | filter_node *next = p->next; |
| 222 | gpr_free(p); |
| 223 | p = next; |
| 224 | } |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 225 | if (builder->args != NULL) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 226 | grpc_channel_args_destroy(exec_ctx, builder->args); |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 227 | } |
| 228 | gpr_free(builder->target); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 229 | gpr_free(builder); |
| 230 | } |
| 231 | |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 232 | grpc_error *grpc_channel_stack_builder_finish( |
| 233 | grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder, |
| 234 | size_t prefix_bytes, int initial_refs, grpc_iomgr_cb_func destroy, |
| 235 | void *destroy_arg, void **result) { |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 236 | // count the number of filters |
| 237 | size_t num_filters = 0; |
| 238 | for (filter_node *p = builder->begin.next; p != &builder->end; p = p->next) { |
| 239 | num_filters++; |
| 240 | } |
| 241 | |
| 242 | // create an array of filters |
Craig Tiller | f6a4d42 | 2016-02-18 08:21:21 -0800 | [diff] [blame] | 243 | const grpc_channel_filter **filters = |
| 244 | gpr_malloc(sizeof(*filters) * num_filters); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 245 | size_t i = 0; |
| 246 | for (filter_node *p = builder->begin.next; p != &builder->end; p = p->next) { |
| 247 | filters[i++] = p->filter; |
| 248 | } |
| 249 | |
| 250 | // calculate the size of the channel stack |
| 251 | size_t channel_stack_size = grpc_channel_stack_size(filters, num_filters); |
| 252 | |
| 253 | // allocate memory, with prefix_bytes followed by channel_stack_size |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 254 | *result = gpr_malloc(prefix_bytes + channel_stack_size); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 255 | // fetch a pointer to the channel stack |
| 256 | grpc_channel_stack *channel_stack = |
Mark D. Roth | e854776 | 2016-11-28 17:42:53 +0000 | [diff] [blame] | 257 | (grpc_channel_stack *)((char *)(*result) + prefix_bytes); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 258 | // and initialize it |
Mark D. Roth | c108788 | 2016-11-18 10:54:45 -0800 | [diff] [blame] | 259 | grpc_error *error = grpc_channel_stack_init( |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 260 | exec_ctx, initial_refs, destroy, |
| 261 | destroy_arg == NULL ? *result : destroy_arg, filters, num_filters, |
| 262 | builder->args, builder->transport, builder->name, channel_stack); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 263 | |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 264 | if (error != GRPC_ERROR_NONE) { |
| 265 | grpc_channel_stack_destroy(exec_ctx, channel_stack); |
| 266 | gpr_free(*result); |
| 267 | *result = NULL; |
| 268 | } else { |
| 269 | // run post-initialization functions |
| 270 | i = 0; |
Mark D. Roth | 977f5d4 | 2016-11-30 14:03:58 -0800 | [diff] [blame] | 271 | for (filter_node *p = builder->begin.next; p != &builder->end; |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 272 | p = p->next) { |
| 273 | if (p->init != NULL) { |
| 274 | p->init(channel_stack, grpc_channel_stack_element(channel_stack, i), |
| 275 | p->init_arg); |
| 276 | } |
| 277 | i++; |
Craig Tiller | de67626 | 2016-02-19 12:28:34 -0800 | [diff] [blame] | 278 | } |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 281 | grpc_channel_stack_builder_destroy(exec_ctx, builder); |
Craig Tiller | f6a4d42 | 2016-02-18 08:21:21 -0800 | [diff] [blame] | 282 | gpr_free((grpc_channel_filter **)filters); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 283 | |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 284 | return error; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 285 | } |