blob: d91a65cb7035cff3aca24416028ed5e5b4beaded [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
David Garcia Quintas3598d442016-03-15 14:53:05 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H
35#define GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
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 Tiller6e7c6222015-02-20 15:31:21 -080048#include "src/core/debug/trace.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049#include "src/core/transport/transport.h"
50
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051typedef struct grpc_channel_element grpc_channel_element;
52typedef struct grpc_call_element grpc_call_element;
53
Craig Tiller906e3bc2015-11-24 07:31:31 -080054typedef struct grpc_channel_stack grpc_channel_stack;
55typedef struct grpc_call_stack grpc_call_stack;
56
Craig Tiller577c9b22015-11-02 14:11:15 -080057typedef struct {
Craig Tiller906e3bc2015-11-24 07:31:31 -080058 grpc_channel_stack *channel_stack;
Craig Tiller577c9b22015-11-02 14:11:15 -080059 const grpc_channel_args *channel_args;
Craig Tiller577c9b22015-11-02 14:11:15 -080060 int is_first;
61 int is_last;
62} grpc_channel_element_args;
63
64typedef struct {
Craig Tiller906e3bc2015-11-24 07:31:31 -080065 grpc_call_stack *call_stack;
Craig Tiller577c9b22015-11-02 14:11:15 -080066 const void *server_transport_data;
67 grpc_call_context_element *context;
68} grpc_call_element_args;
69
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070/* 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. */
Craig Tillera82950e2015-09-22 12:33:20 -070080typedef struct {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 /* 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 */
Craig Tillera82950e2015-09-22 12:33:20 -070083 void (*start_transport_stream_op)(grpc_exec_ctx *exec_ctx,
84 grpc_call_element *elem,
85 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 /* 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 */
Craig Tillera82950e2015-09-22 12:33:20 -070089 void (*start_transport_op)(grpc_exec_ctx *exec_ctx,
90 grpc_channel_element *elem, grpc_transport_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091
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
Craig Tiller45724b32015-09-22 10:42:19 -0700101 argument. */
Craig Tillera82950e2015-09-22 12:33:20 -0700102 void (*init_call_elem)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800103 grpc_call_element_args *args);
104 void (*set_pollset)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
105 grpc_pollset *pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 /* Destroy per call data.
107 The filter does not need to do any chaining */
Craig Tillera82950e2015-09-22 12:33:20 -0700108 void (*destroy_call_elem)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109
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 */
Craig Tillera82950e2015-09-22 12:33:20 -0700118 void (*init_channel_elem)(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800119 grpc_channel_element_args *args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 /* Destroy per channel data.
121 The filter does not need to do any chaining */
Craig Tillera82950e2015-09-22 12:33:20 -0700122 void (*destroy_channel_elem)(grpc_exec_ctx *exec_ctx,
123 grpc_channel_element *elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700125 /* Implement grpc_call_get_peer() */
Craig Tillera82950e2015-09-22 12:33:20 -0700126 char *(*get_peer)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700127
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128 /* 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 */
Craig Tillera82950e2015-09-22 12:33:20 -0700134struct grpc_channel_element {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135 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 */
Craig Tillera82950e2015-09-22 12:33:20 -0700142struct grpc_call_element {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143 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 */
Craig Tiller906e3bc2015-11-24 07:31:31 -0800150struct grpc_channel_stack {
151 grpc_stream_refcount refcount;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152 size_t count;
153 /* Memory required for a call stack (computed at channel stack
154 initialization) */
155 size_t call_stack_size;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800156};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157
158/* A call stack tracks a set of related filters for one call, and guarantees
159 they live within a single malloc() allocation */
Craig Tiller906e3bc2015-11-24 07:31:31 -0800160struct grpc_call_stack {
Craig Tiller577c9b22015-11-02 14:11:15 -0800161 /* 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;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800167};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168
169/* Get a channel element given a channel stack and its index */
Craig Tillera82950e2015-09-22 12:33:20 -0700170grpc_channel_element *grpc_channel_stack_element(grpc_channel_stack *stack,
171 size_t i);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172/* Get the last channel element in a channel stack */
Craig Tillera82950e2015-09-22 12:33:20 -0700173grpc_channel_element *grpc_channel_stack_last_element(
174 grpc_channel_stack *stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175/* Get a call stack element given a call stack and an index */
Craig Tillera82950e2015-09-22 12:33:20 -0700176grpc_call_element *grpc_call_stack_element(grpc_call_stack *stack, size_t i);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
178/* Determine memory required for a channel stack containing a set of filters */
Craig Tillera82950e2015-09-22 12:33:20 -0700179size_t grpc_channel_stack_size(const grpc_channel_filter **filters,
180 size_t filter_count);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181/* Initialize a channel stack given some filters */
Craig Tiller7b435612015-11-24 08:15:05 -0800182void grpc_channel_stack_init(grpc_exec_ctx *exec_ctx, int initial_refs,
183 grpc_iomgr_cb_func destroy, void *destroy_arg,
Craig Tillera82950e2015-09-22 12:33:20 -0700184 const grpc_channel_filter **filters,
Craig Tiller7b435612015-11-24 08:15:05 -0800185 size_t filter_count, const grpc_channel_args *args,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800186 const char *name, grpc_channel_stack *stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187/* Destroy a channel stack */
Craig Tillera82950e2015-09-22 12:33:20 -0700188void grpc_channel_stack_destroy(grpc_exec_ctx *exec_ctx,
189 grpc_channel_stack *stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800190
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. */
Craig Tillera82950e2015-09-22 12:33:20 -0700194void grpc_call_stack_init(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800195 grpc_channel_stack *channel_stack, int initial_refs,
196 grpc_iomgr_cb_func destroy, void *destroy_arg,
197 grpc_call_context_element *context,
Craig Tillera82950e2015-09-22 12:33:20 -0700198 const void *transport_server_data,
Craig Tillera82950e2015-09-22 12:33:20 -0700199 grpc_call_stack *call_stack);
Craig Tiller577c9b22015-11-02 14:11:15 -0800200/* Set a pollset for a call stack: must occur before the first op is started */
201void 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
Craig Tiller906e3bc2015-11-24 07:31:31 -0800206#define GRPC_CALL_STACK_REF(call_stack, reason) \
Craig Tiller577c9b22015-11-02 14:11:15 -0800207 grpc_stream_ref(&(call_stack)->refcount, reason)
Craig Tiller906e3bc2015-11-24 07:31:31 -0800208#define GRPC_CALL_STACK_UNREF(exec_ctx, call_stack, reason) \
Craig Tiller577c9b22015-11-02 14:11:15 -0800209 grpc_stream_unref(exec_ctx, &(call_stack)->refcount, reason)
Craig Tiller906e3bc2015-11-24 07:31:31 -0800210#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)
Craig Tiller577c9b22015-11-02 14:11:15 -0800214#else
Craig Tiller7b435612015-11-24 08:15:05 -0800215#define GRPC_CALL_STACK_REF(call_stack, reason) \
216 grpc_stream_ref(&(call_stack)->refcount)
Craig Tiller906e3bc2015-11-24 07:31:31 -0800217#define GRPC_CALL_STACK_UNREF(exec_ctx, call_stack, reason) \
Craig Tiller577c9b22015-11-02 14:11:15 -0800218 grpc_stream_unref(exec_ctx, &(call_stack)->refcount)
Craig Tiller7b435612015-11-24 08:15:05 -0800219#define GRPC_CHANNEL_STACK_REF(channel_stack, reason) \
220 grpc_stream_ref(&(channel_stack)->refcount)
Craig Tiller906e3bc2015-11-24 07:31:31 -0800221#define GRPC_CHANNEL_STACK_UNREF(exec_ctx, channel_stack, reason) \
222 grpc_stream_unref(exec_ctx, &(channel_stack)->refcount)
Craig Tiller577c9b22015-11-02 14:11:15 -0800223#endif
224
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225/* Destroy a call stack */
Craig Tillera82950e2015-09-22 12:33:20 -0700226void grpc_call_stack_destroy(grpc_exec_ctx *exec_ctx, grpc_call_stack *stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227
Craig Tiller892f2d32015-11-08 13:53:04 +0000228/* Ignore set pollset - used by filters to implement the set_pollset method
229 if they don't care about pollsets at all. Does nothing. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800230void grpc_call_stack_ignore_set_pollset(grpc_exec_ctx *exec_ctx,
231 grpc_call_element *elem,
232 grpc_pollset *pollset);
Craig Tiller83f88d92015-04-21 16:02:05 -0700233/* Call the next operation in a call stack */
Craig Tillera82950e2015-09-22 12:33:20 -0700234void grpc_call_next_op(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
235 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236/* Call the next operation (depending on call directionality) in a channel
237 stack */
Craig Tillera82950e2015-09-22 12:33:20 -0700238void grpc_channel_next_op(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
239 grpc_transport_op *op);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700240/* Pass through a request to get_peer to the next child element */
Craig Tillera82950e2015-09-22 12:33:20 -0700241char *grpc_call_next_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242
243/* Given the top element of a channel stack, get the channel stack itself */
Craig Tillera82950e2015-09-22 12:33:20 -0700244grpc_channel_stack *grpc_channel_stack_from_top_element(
245 grpc_channel_element *elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246/* Given the top element of a call stack, get the call stack itself */
Craig Tillera82950e2015-09-22 12:33:20 -0700247grpc_call_stack *grpc_call_stack_from_top_element(grpc_call_element *elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800248
Craig Tillera82950e2015-09-22 12:33:20 -0700249void grpc_call_log_op(char *file, int line, gpr_log_severity severity,
250 grpc_call_element *elem, grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251
Craig Tillera82950e2015-09-22 12:33:20 -0700252void grpc_call_element_send_cancel(grpc_exec_ctx *exec_ctx,
253 grpc_call_element *cur_elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800254
Craig Tillerfaa84802015-03-01 21:56:38 -0800255extern int grpc_trace_channel;
256
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800257#define GRPC_CALL_LOG_OP(sev, elem, op) \
Craig Tillerfaa84802015-03-01 21:56:38 -0800258 if (grpc_trace_channel) grpc_call_log_op(sev, elem, op)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800259
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700260#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H */