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 <grpc/grpc.h>
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27
28 #include "src/core/lib/channel/channel_stack.h"
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/gprpp/atomic.h"
31 #include "src/core/lib/surface/api_trace.h"
32 #include "src/core/lib/surface/call.h"
33 #include "src/core/lib/surface/channel.h"
34 #include "src/core/lib/surface/lame_client.h"
35 #include "src/core/lib/transport/connectivity_state.h"
36 #include "src/core/lib/transport/static_metadata.h"
37
38 namespace grpc_core {
39
40 namespace {
41
42 struct CallData {
43 CallCombiner* call_combiner;
44 grpc_linked_mdelem status;
45 grpc_linked_mdelem details;
46 Atomic<bool> filled_metadata;
47 };
48
49 struct ChannelData {
ChannelDatagrpc_core::__anon4963cf120111::ChannelData50 ChannelData() : state_tracker("lame_channel", GRPC_CHANNEL_SHUTDOWN) {}
51
52 grpc_status_code error_code;
53 const char* error_message;
54 Mutex mu;
55 ConnectivityStateTracker state_tracker;
56 };
57
fill_metadata(grpc_call_element * elem,grpc_metadata_batch * mdb)58 static void fill_metadata(grpc_call_element* elem, grpc_metadata_batch* mdb) {
59 CallData* calld = static_cast<CallData*>(elem->call_data);
60 bool expected = false;
61 if (!calld->filled_metadata.CompareExchangeStrong(
62 &expected, true, MemoryOrder::RELAXED, MemoryOrder::RELAXED)) {
63 return;
64 }
65 ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
66 char tmp[GPR_LTOA_MIN_BUFSIZE];
67 gpr_ltoa(chand->error_code, tmp);
68 calld->status.md = grpc_mdelem_from_slices(
69 GRPC_MDSTR_GRPC_STATUS, grpc_core::UnmanagedMemorySlice(tmp));
70 calld->details.md = grpc_mdelem_from_slices(
71 GRPC_MDSTR_GRPC_MESSAGE,
72 grpc_core::UnmanagedMemorySlice(chand->error_message));
73 calld->status.prev = calld->details.next = nullptr;
74 calld->status.next = &calld->details;
75 calld->details.prev = &calld->status;
76 mdb->list.head = &calld->status;
77 mdb->list.tail = &calld->details;
78 mdb->list.count = 2;
79 mdb->deadline = GRPC_MILLIS_INF_FUTURE;
80 }
81
lame_start_transport_stream_op_batch(grpc_call_element * elem,grpc_transport_stream_op_batch * op)82 static void lame_start_transport_stream_op_batch(
83 grpc_call_element* elem, grpc_transport_stream_op_batch* op) {
84 CallData* calld = static_cast<CallData*>(elem->call_data);
85 if (op->recv_initial_metadata) {
86 fill_metadata(elem,
87 op->payload->recv_initial_metadata.recv_initial_metadata);
88 } else if (op->recv_trailing_metadata) {
89 fill_metadata(elem,
90 op->payload->recv_trailing_metadata.recv_trailing_metadata);
91 }
92 grpc_transport_stream_op_batch_finish_with_failure(
93 op, GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"),
94 calld->call_combiner);
95 }
96
lame_get_channel_info(grpc_channel_element *,const grpc_channel_info *)97 static void lame_get_channel_info(grpc_channel_element* /*elem*/,
98 const grpc_channel_info* /*channel_info*/) {}
99
lame_start_transport_op(grpc_channel_element * elem,grpc_transport_op * op)100 static void lame_start_transport_op(grpc_channel_element* elem,
101 grpc_transport_op* op) {
102 ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
103 {
104 MutexLock lock(&chand->mu);
105 if (op->start_connectivity_watch != nullptr) {
106 chand->state_tracker.AddWatcher(op->start_connectivity_watch_state,
107 std::move(op->start_connectivity_watch));
108 }
109 if (op->stop_connectivity_watch != nullptr) {
110 chand->state_tracker.RemoveWatcher(op->stop_connectivity_watch);
111 }
112 }
113 if (op->send_ping.on_initiate != nullptr) {
114 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_initiate,
115 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
116 }
117 if (op->send_ping.on_ack != nullptr) {
118 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_ack,
119 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
120 }
121 GRPC_ERROR_UNREF(op->disconnect_with_error);
122 if (op->on_consumed != nullptr) {
123 ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, GRPC_ERROR_NONE);
124 }
125 }
126
lame_init_call_elem(grpc_call_element * elem,const grpc_call_element_args * args)127 static grpc_error* lame_init_call_elem(grpc_call_element* elem,
128 const grpc_call_element_args* args) {
129 CallData* calld = static_cast<CallData*>(elem->call_data);
130 calld->call_combiner = args->call_combiner;
131 return GRPC_ERROR_NONE;
132 }
133
lame_destroy_call_elem(grpc_call_element *,const grpc_call_final_info *,grpc_closure * then_schedule_closure)134 static void lame_destroy_call_elem(grpc_call_element* /*elem*/,
135 const grpc_call_final_info* /*final_info*/,
136 grpc_closure* then_schedule_closure) {
137 ExecCtx::Run(DEBUG_LOCATION, then_schedule_closure, GRPC_ERROR_NONE);
138 }
139
lame_init_channel_elem(grpc_channel_element * elem,grpc_channel_element_args * args)140 static grpc_error* lame_init_channel_elem(grpc_channel_element* elem,
141 grpc_channel_element_args* args) {
142 GPR_ASSERT(args->is_first);
143 GPR_ASSERT(args->is_last);
144 new (elem->channel_data) ChannelData;
145 return GRPC_ERROR_NONE;
146 }
147
lame_destroy_channel_elem(grpc_channel_element * elem)148 static void lame_destroy_channel_elem(grpc_channel_element* elem) {
149 ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
150 chand->~ChannelData();
151 }
152
153 } // namespace
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_channel_element* elem;
178 grpc_channel* channel =
179 grpc_channel_create(target, nullptr, GRPC_CLIENT_LAME_CHANNEL, nullptr);
180 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
181 GRPC_API_TRACE(
182 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
183 "error_message=%s)",
184 3, (target, (int)error_code, error_message));
185 GPR_ASSERT(elem->filter == &grpc_lame_filter);
186 auto chand = static_cast<grpc_core::ChannelData*>(elem->channel_data);
187 chand->error_code = error_code;
188 chand->error_message = error_message;
189
190 return channel;
191 }
192