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/surface/lame_client.h"
22
23 #include <string.h>
24
25 #include <atomic>
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30
31 #include "src/core/lib/channel/channel_stack.h"
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/surface/api_trace.h"
34 #include "src/core/lib/surface/call.h"
35 #include "src/core/lib/surface/channel.h"
36 #include "src/core/lib/transport/connectivity_state.h"
37 #include "src/core/lib/transport/static_metadata.h"
38
39 #define GRPC_ARG_LAME_FILTER_ERROR "grpc.lame_filter_error"
40
41 namespace grpc_core {
42
43 namespace {
44
45 struct ChannelData {
ChannelDatagrpc_core::__anonbbf343fe0111::ChannelData46 explicit ChannelData(grpc_channel_element_args* args)
47 : state_tracker("lame_channel", GRPC_CHANNEL_SHUTDOWN) {
48 grpc_error_handle* err = grpc_channel_args_find_pointer<grpc_error_handle>(
49 args->channel_args, GRPC_ARG_LAME_FILTER_ERROR);
50 if (err != nullptr) error = GRPC_ERROR_REF(*err);
51 }
52
~ChannelDatagrpc_core::__anonbbf343fe0111::ChannelData53 ~ChannelData() { GRPC_ERROR_UNREF(error); }
54
55 grpc_error_handle error = GRPC_ERROR_NONE;
56 Mutex mu;
57 ConnectivityStateTracker state_tracker;
58 };
59
60 struct CallData {
61 CallCombiner* call_combiner;
62 };
63
lame_start_transport_stream_op_batch(grpc_call_element * elem,grpc_transport_stream_op_batch * op)64 static void lame_start_transport_stream_op_batch(
65 grpc_call_element* elem, grpc_transport_stream_op_batch* op) {
66 CallData* calld = static_cast<CallData*>(elem->call_data);
67 ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
68 grpc_transport_stream_op_batch_finish_with_failure(
69 op, GRPC_ERROR_REF(chand->error), calld->call_combiner);
70 }
71
lame_get_channel_info(grpc_channel_element *,const grpc_channel_info *)72 static void lame_get_channel_info(grpc_channel_element* /*elem*/,
73 const grpc_channel_info* /*channel_info*/) {}
74
lame_start_transport_op(grpc_channel_element * elem,grpc_transport_op * op)75 static void lame_start_transport_op(grpc_channel_element* elem,
76 grpc_transport_op* op) {
77 ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
78 {
79 MutexLock lock(&chand->mu);
80 if (op->start_connectivity_watch != nullptr) {
81 chand->state_tracker.AddWatcher(op->start_connectivity_watch_state,
82 std::move(op->start_connectivity_watch));
83 }
84 if (op->stop_connectivity_watch != nullptr) {
85 chand->state_tracker.RemoveWatcher(op->stop_connectivity_watch);
86 }
87 }
88 if (op->send_ping.on_initiate != nullptr) {
89 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_initiate,
90 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
91 }
92 if (op->send_ping.on_ack != nullptr) {
93 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_ack,
94 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
95 }
96 GRPC_ERROR_UNREF(op->disconnect_with_error);
97 if (op->on_consumed != nullptr) {
98 ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, GRPC_ERROR_NONE);
99 }
100 }
101
lame_init_call_elem(grpc_call_element * elem,const grpc_call_element_args * args)102 static grpc_error_handle lame_init_call_elem(
103 grpc_call_element* elem, const grpc_call_element_args* args) {
104 CallData* calld = static_cast<CallData*>(elem->call_data);
105 calld->call_combiner = args->call_combiner;
106 return GRPC_ERROR_NONE;
107 }
108
lame_destroy_call_elem(grpc_call_element *,const grpc_call_final_info *,grpc_closure * then_schedule_closure)109 static void lame_destroy_call_elem(grpc_call_element* /*elem*/,
110 const grpc_call_final_info* /*final_info*/,
111 grpc_closure* then_schedule_closure) {
112 ExecCtx::Run(DEBUG_LOCATION, then_schedule_closure, GRPC_ERROR_NONE);
113 }
114
lame_init_channel_elem(grpc_channel_element * elem,grpc_channel_element_args * args)115 static grpc_error_handle lame_init_channel_elem(
116 grpc_channel_element* elem, grpc_channel_element_args* args) {
117 new (elem->channel_data) ChannelData(args);
118 return GRPC_ERROR_NONE;
119 }
120
lame_destroy_channel_elem(grpc_channel_element * elem)121 static void lame_destroy_channel_elem(grpc_channel_element* elem) {
122 ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
123 chand->~ChannelData();
124 }
125
126 // Channel arg vtable for a grpc_error_handle.
ErrorCopy(void * p)127 void* ErrorCopy(void* p) {
128 grpc_error_handle* new_error = nullptr;
129 if (p != nullptr) {
130 grpc_error_handle* error = static_cast<grpc_error_handle*>(p);
131 new_error = new grpc_error_handle();
132 *new_error = GRPC_ERROR_REF(*error);
133 }
134 return new_error;
135 }
ErrorDestroy(void * p)136 void ErrorDestroy(void* p) {
137 if (p != nullptr) {
138 grpc_error_handle* error = static_cast<grpc_error_handle*>(p);
139 GRPC_ERROR_UNREF(*error);
140 delete error;
141 }
142 }
ErrorCompare(void * p,void * q)143 int ErrorCompare(void* p, void* q) { return grpc_core::QsortCompare(p, q); }
144 const grpc_arg_pointer_vtable kLameFilterErrorArgVtable = {
145 ErrorCopy, ErrorDestroy, ErrorCompare};
146
147 } // namespace
148
MakeLameClientErrorArg(grpc_error_handle * error)149 grpc_arg MakeLameClientErrorArg(grpc_error_handle* error) {
150 return grpc_channel_arg_pointer_create(
151 const_cast<char*>(GRPC_ARG_LAME_FILTER_ERROR), error,
152 &kLameFilterErrorArgVtable);
153 }
154
155 } // namespace grpc_core
156
157 const grpc_channel_filter grpc_lame_filter = {
158 grpc_core::lame_start_transport_stream_op_batch,
159 grpc_core::lame_start_transport_op,
160 sizeof(grpc_core::CallData),
161 grpc_core::lame_init_call_elem,
162 grpc_call_stack_ignore_set_pollset_or_pollset_set,
163 grpc_core::lame_destroy_call_elem,
164 sizeof(grpc_core::ChannelData),
165 grpc_core::lame_init_channel_elem,
166 grpc_core::lame_destroy_channel_elem,
167 grpc_core::lame_get_channel_info,
168 "lame-client",
169 };
170
171 #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack*)((c) + 1))
172
grpc_lame_client_channel_create(const char * target,grpc_status_code error_code,const char * error_message)173 grpc_channel* grpc_lame_client_channel_create(const char* target,
174 grpc_status_code error_code,
175 const char* error_message) {
176 grpc_core::ExecCtx exec_ctx;
177 GRPC_API_TRACE(
178 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
179 "error_message=%s)",
180 3, (target, (int)error_code, error_message));
181 grpc_error_handle error = grpc_error_set_str(
182 grpc_error_set_int(
183 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"),
184 GRPC_ERROR_INT_GRPC_STATUS, error_code),
185 GRPC_ERROR_STR_GRPC_MESSAGE, error_message);
186 grpc_arg error_arg = grpc_core::MakeLameClientErrorArg(&error);
187 grpc_channel_args args = {1, &error_arg};
188 grpc_channel* channel = grpc_channel_create(
189 target, &args, GRPC_CLIENT_LAME_CHANNEL, nullptr, nullptr, 0, nullptr);
190 GRPC_ERROR_UNREF(error);
191 return channel;
192 }
193