• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/channel/connected_channel.h"
22 
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <grpc/byte_buffer.h>
28 #include <grpc/slice_buffer.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/profiling/timers.h"
33 #include "src/core/lib/transport/transport.h"
34 
35 #define MAX_BUFFER_LENGTH 8192
36 
37 typedef struct connected_channel_channel_data {
38   grpc_transport* transport;
39 } channel_data;
40 
41 struct callback_state {
42   grpc_closure closure;
43   grpc_closure* original_closure;
44   grpc_core::CallCombiner* call_combiner;
45   const char* reason;
46 };
47 typedef struct connected_channel_call_data {
48   grpc_core::CallCombiner* call_combiner;
49   // Closures used for returning results on the call combiner.
50   callback_state on_complete[6];  // Max number of pending batches.
51   callback_state recv_initial_metadata_ready;
52   callback_state recv_message_ready;
53   callback_state recv_trailing_metadata_ready;
54 } call_data;
55 
run_in_call_combiner(void * arg,grpc_error * error)56 static void run_in_call_combiner(void* arg, grpc_error* error) {
57   callback_state* state = static_cast<callback_state*>(arg);
58   GRPC_CALL_COMBINER_START(state->call_combiner, state->original_closure,
59                            GRPC_ERROR_REF(error), state->reason);
60 }
61 
run_cancel_in_call_combiner(void * arg,grpc_error * error)62 static void run_cancel_in_call_combiner(void* arg, grpc_error* error) {
63   run_in_call_combiner(arg, error);
64   gpr_free(arg);
65 }
66 
intercept_callback(call_data * calld,callback_state * state,bool free_when_done,const char * reason,grpc_closure ** original_closure)67 static void intercept_callback(call_data* calld, callback_state* state,
68                                bool free_when_done, const char* reason,
69                                grpc_closure** original_closure) {
70   state->original_closure = *original_closure;
71   state->call_combiner = calld->call_combiner;
72   state->reason = reason;
73   *original_closure = GRPC_CLOSURE_INIT(
74       &state->closure,
75       free_when_done ? run_cancel_in_call_combiner : run_in_call_combiner,
76       state, grpc_schedule_on_exec_ctx);
77 }
78 
get_state_for_batch(call_data * calld,grpc_transport_stream_op_batch * batch)79 static callback_state* get_state_for_batch(
80     call_data* calld, grpc_transport_stream_op_batch* batch) {
81   if (batch->send_initial_metadata) return &calld->on_complete[0];
82   if (batch->send_message) return &calld->on_complete[1];
83   if (batch->send_trailing_metadata) return &calld->on_complete[2];
84   if (batch->recv_initial_metadata) return &calld->on_complete[3];
85   if (batch->recv_message) return &calld->on_complete[4];
86   if (batch->recv_trailing_metadata) return &calld->on_complete[5];
87   GPR_UNREACHABLE_CODE(return nullptr);
88 }
89 
90 /* We perform a small hack to locate transport data alongside the connected
91    channel data in call allocations, to allow everything to be pulled in minimal
92    cache line requests */
93 #define TRANSPORT_STREAM_FROM_CALL_DATA(calld) \
94   ((grpc_stream*)(((char*)(calld)) +           \
95                   GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(call_data))))
96 #define CALL_DATA_FROM_TRANSPORT_STREAM(transport_stream) \
97   ((call_data*)(((char*)(transport_stream)) -             \
98                 GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(call_data))))
99 
100 /* Intercept a call operation and either push it directly up or translate it
101    into transport stream operations */
connected_channel_start_transport_stream_op_batch(grpc_call_element * elem,grpc_transport_stream_op_batch * batch)102 static void connected_channel_start_transport_stream_op_batch(
103     grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
104   call_data* calld = static_cast<call_data*>(elem->call_data);
105   channel_data* chand = static_cast<channel_data*>(elem->channel_data);
106   if (batch->recv_initial_metadata) {
107     callback_state* state = &calld->recv_initial_metadata_ready;
108     intercept_callback(
109         calld, state, false, "recv_initial_metadata_ready",
110         &batch->payload->recv_initial_metadata.recv_initial_metadata_ready);
111   }
112   if (batch->recv_message) {
113     callback_state* state = &calld->recv_message_ready;
114     intercept_callback(calld, state, false, "recv_message_ready",
115                        &batch->payload->recv_message.recv_message_ready);
116   }
117   if (batch->recv_trailing_metadata) {
118     callback_state* state = &calld->recv_trailing_metadata_ready;
119     intercept_callback(
120         calld, state, false, "recv_trailing_metadata_ready",
121         &batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready);
122   }
123   if (batch->cancel_stream) {
124     // There can be more than one cancellation batch in flight at any
125     // given time, so we can't just pick out a fixed index into
126     // calld->on_complete like we can for the other ops.  However,
127     // cancellation isn't in the fast path, so we just allocate a new
128     // closure for each one.
129     callback_state* state =
130         static_cast<callback_state*>(gpr_malloc(sizeof(*state)));
131     intercept_callback(calld, state, true, "on_complete (cancel_stream)",
132                        &batch->on_complete);
133   } else if (batch->on_complete != nullptr) {
134     callback_state* state = get_state_for_batch(calld, batch);
135     intercept_callback(calld, state, false, "on_complete", &batch->on_complete);
136   }
137   grpc_transport_perform_stream_op(
138       chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA(calld), batch);
139   GRPC_CALL_COMBINER_STOP(calld->call_combiner, "passed batch to transport");
140 }
141 
connected_channel_start_transport_op(grpc_channel_element * elem,grpc_transport_op * op)142 static void connected_channel_start_transport_op(grpc_channel_element* elem,
143                                                  grpc_transport_op* op) {
144   channel_data* chand = static_cast<channel_data*>(elem->channel_data);
145   grpc_transport_perform_op(chand->transport, op);
146 }
147 
148 /* Constructor for call_data */
connected_channel_init_call_elem(grpc_call_element * elem,const grpc_call_element_args * args)149 static grpc_error* connected_channel_init_call_elem(
150     grpc_call_element* elem, const grpc_call_element_args* args) {
151   call_data* calld = static_cast<call_data*>(elem->call_data);
152   channel_data* chand = static_cast<channel_data*>(elem->channel_data);
153   calld->call_combiner = args->call_combiner;
154   int r = grpc_transport_init_stream(
155       chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA(calld),
156       &args->call_stack->refcount, args->server_transport_data, args->arena);
157   return r == 0 ? GRPC_ERROR_NONE
158                 : GRPC_ERROR_CREATE_FROM_STATIC_STRING(
159                       "transport stream initialization failed");
160 }
161 
set_pollset_or_pollset_set(grpc_call_element * elem,grpc_polling_entity * pollent)162 static void set_pollset_or_pollset_set(grpc_call_element* elem,
163                                        grpc_polling_entity* pollent) {
164   call_data* calld = static_cast<call_data*>(elem->call_data);
165   channel_data* chand = static_cast<channel_data*>(elem->channel_data);
166   grpc_transport_set_pops(chand->transport,
167                           TRANSPORT_STREAM_FROM_CALL_DATA(calld), pollent);
168 }
169 
170 /* Destructor for call_data */
connected_channel_destroy_call_elem(grpc_call_element * elem,const grpc_call_final_info *,grpc_closure * then_schedule_closure)171 static void connected_channel_destroy_call_elem(
172     grpc_call_element* elem, const grpc_call_final_info* /*final_info*/,
173     grpc_closure* then_schedule_closure) {
174   call_data* calld = static_cast<call_data*>(elem->call_data);
175   channel_data* chand = static_cast<channel_data*>(elem->channel_data);
176   grpc_transport_destroy_stream(chand->transport,
177                                 TRANSPORT_STREAM_FROM_CALL_DATA(calld),
178                                 then_schedule_closure);
179 }
180 
181 /* Constructor for channel_data */
connected_channel_init_channel_elem(grpc_channel_element * elem,grpc_channel_element_args * args)182 static grpc_error* connected_channel_init_channel_elem(
183     grpc_channel_element* elem, grpc_channel_element_args* args) {
184   channel_data* cd = static_cast<channel_data*>(elem->channel_data);
185   GPR_ASSERT(args->is_last);
186   cd->transport = nullptr;
187   return GRPC_ERROR_NONE;
188 }
189 
190 /* Destructor for channel_data */
connected_channel_destroy_channel_elem(grpc_channel_element * elem)191 static void connected_channel_destroy_channel_elem(grpc_channel_element* elem) {
192   channel_data* cd = static_cast<channel_data*>(elem->channel_data);
193   if (cd->transport) {
194     grpc_transport_destroy(cd->transport);
195   }
196 }
197 
198 /* No-op. */
connected_channel_get_channel_info(grpc_channel_element *,const grpc_channel_info *)199 static void connected_channel_get_channel_info(
200     grpc_channel_element* /*elem*/, const grpc_channel_info* /*channel_info*/) {
201 }
202 
203 const grpc_channel_filter grpc_connected_filter = {
204     connected_channel_start_transport_stream_op_batch,
205     connected_channel_start_transport_op,
206     sizeof(call_data),
207     connected_channel_init_call_elem,
208     set_pollset_or_pollset_set,
209     connected_channel_destroy_call_elem,
210     sizeof(channel_data),
211     connected_channel_init_channel_elem,
212     connected_channel_destroy_channel_elem,
213     connected_channel_get_channel_info,
214     "connected",
215 };
216 
bind_transport(grpc_channel_stack * channel_stack,grpc_channel_element * elem,void * t)217 static void bind_transport(grpc_channel_stack* channel_stack,
218                            grpc_channel_element* elem, void* t) {
219   channel_data* cd = static_cast<channel_data*>(elem->channel_data);
220   GPR_ASSERT(elem->filter == &grpc_connected_filter);
221   GPR_ASSERT(cd->transport == nullptr);
222   cd->transport = static_cast<grpc_transport*>(t);
223 
224   /* HACK(ctiller): increase call stack size for the channel to make space
225      for channel data. We need a cleaner (but performant) way to do this,
226      and I'm not sure what that is yet.
227      This is only "safe" because call stacks place no additional data after
228      the last call element, and the last call element MUST be the connected
229      channel. */
230   channel_stack->call_stack_size +=
231       grpc_transport_stream_size(static_cast<grpc_transport*>(t));
232 }
233 
grpc_add_connected_filter(grpc_channel_stack_builder * builder,void * arg_must_be_null)234 bool grpc_add_connected_filter(grpc_channel_stack_builder* builder,
235                                void* arg_must_be_null) {
236   GPR_ASSERT(arg_must_be_null == nullptr);
237   grpc_transport* t = grpc_channel_stack_builder_get_transport(builder);
238   GPR_ASSERT(t != nullptr);
239   return grpc_channel_stack_builder_append_filter(
240       builder, &grpc_connected_filter, bind_transport, t);
241 }
242 
grpc_connected_channel_get_stream(grpc_call_element * elem)243 grpc_stream* grpc_connected_channel_get_stream(grpc_call_element* elem) {
244   call_data* calld = static_cast<call_data*>(elem->call_data);
245   return TRANSPORT_STREAM_FROM_CALL_DATA(calld);
246 }
247