Debian Codegraph @

test/core/util/passthru_endpoint.cc

raw
1 /*
2 *
3 * Copyright 2016 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 "test/core/util/passthru_endpoint.h"
20
21 #include <string.h>
22
23 #include <algorithm>
24 #include <functional>
25 #include <string>
26
27 #include "absl/status/status.h"
28 #include "absl/strings/str_format.h"
29 #include "absl/strings/string_view.h"
30
31 #include <grpc/slice.h>
32 #include <grpc/slice_buffer.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35
36 #include "src/core/lib/gprpp/debug_location.h"
37 #include "src/core/lib/gprpp/time.h"
38 #include "src/core/lib/iomgr/closure.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/exec_ctx.h"
41 #include "src/core/lib/iomgr/iomgr_fwd.h"
42 #include "src/core/lib/iomgr/timer.h"
43
44 typedef struct passthru_endpoint passthru_endpoint;
45
46 typedef struct {
47 bool is_armed;
48 grpc_endpoint* ep;
49 grpc_slice_buffer* slices;
50 grpc_closure* cb;
51 } pending_op;
52
53 typedef struct {
54 grpc_timer timer;
55 uint64_t allowed_write_bytes;
56 uint64_t allowed_read_bytes;
57 std::vector<grpc_passthru_endpoint_channel_action> actions;
58 std::function<void()> on_complete;
59 } grpc_passthru_endpoint_channel_effects;
60
61 typedef struct {
62 grpc_endpoint base;
63 passthru_endpoint* parent;
64 grpc_slice_buffer read_buffer;
65 grpc_slice_buffer write_buffer;
66 grpc_slice_buffer* on_read_out;
67 grpc_closure* on_read;
68 pending_op pending_read_op;
69 pending_op pending_write_op;
70 uint64_t bytes_read_so_far;
71 uint64_t bytes_written_so_far;
72 } half;
73
74 struct passthru_endpoint {
75 gpr_mu mu;
76 int halves;
77 grpc_passthru_endpoint_stats* stats;
78 grpc_passthru_endpoint_channel_effects* channel_effects;
79 bool simulate_channel_actions;
80 bool shutdown;
81 half client;
82 half server;
83 };
84
85 static void do_pending_read_op_locked(half* m, grpc_error_handle error) {
86 GPR_ASSERT(m->pending_read_op.is_armed);
87 GPR_ASSERT(m->bytes_read_so_far <=
88 m->parent->channel_effects->allowed_read_bytes);
89 if (m->parent->shutdown) {
90 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->pending_read_op.cb,
91 GRPC_ERROR_CREATE("Already shutdown"));
92 // Move any pending data into pending_read_op.slices so that it may be
93 // free'ed by the executing callback.
94 grpc_slice_buffer_move_into(&m->read_buffer, m->pending_read_op.slices);
95 m->pending_read_op.is_armed = false;
96 return;
97 }
98
99 if (m->bytes_read_so_far == m->parent->channel_effects->allowed_read_bytes) {
100 // Keep it in pending state.
101 return;
102 }
103 // This delayed processing should only be invoked when read_buffer has
104 // something in it.
105 GPR_ASSERT(m->read_buffer.count > 0);
106 uint64_t readable_length = std::min<uint64_t>(
107 m->read_buffer.length,
108 m->parent->channel_effects->allowed_read_bytes - m->bytes_read_so_far);
109 GPR_ASSERT(readable_length > 0);
110 grpc_slice_buffer_move_first(&m->read_buffer, readable_length,
111 m->pending_read_op.slices);
112 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->pending_read_op.cb, error);
113 if (m->parent->simulate_channel_actions) {
114 m->bytes_read_so_far += readable_length;
115 }
116 m->pending_read_op.is_armed = false;
117 }
118
119 static void me_read(grpc_endpoint* ep, grpc_slice_buffer* slices,
120 grpc_closure* cb, bool /*urgent*/,
121 int /*min_progress_size*/) {
122 half* m = reinterpret_cast<half*>(ep);
123 gpr_mu_lock(&m->parent->mu);
124 if (m->parent->shutdown) {
125 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb,
126 GRPC_ERROR_CREATE("Already shutdown"));
127 } else if (m->read_buffer.count > 0) {
128 GPR_ASSERT(!m->pending_read_op.is_armed);
129 GPR_ASSERT(!m->on_read);
130 m->pending_read_op.is_armed = true;
131 m->pending_read_op.cb = cb;
132 m->pending_read_op.ep = ep;
133 m->pending_read_op.slices = slices;
134 do_pending_read_op_locked(m, absl::OkStatus());
135 } else {
136 GPR_ASSERT(!m->pending_read_op.is_armed);
137 m->on_read = cb;
138 m->on_read_out = slices;
139 }
140 gpr_mu_unlock(&m->parent->mu);
141 }
142
143 // Copy src slice and split the copy at n bytes into two separate slices
144 void grpc_slice_copy_split(grpc_slice src, uint64_t n, grpc_slice& split1,
145 grpc_slice& split2) {
146 GPR_ASSERT(n <= GRPC_SLICE_LENGTH(src));
147 if (n == GRPC_SLICE_LENGTH(src)) {
148 split1 = grpc_slice_copy(src);
149 split2 = grpc_empty_slice();
150 return;
151 }
152 split1 = GRPC_SLICE_MALLOC(n);
153 memcpy(GRPC_SLICE_START_PTR(split1), GRPC_SLICE_START_PTR(src), n);
154 split2 = GRPC_SLICE_MALLOC(GRPC_SLICE_LENGTH(src) - n);
155 memcpy(GRPC_SLICE_START_PTR(split2), GRPC_SLICE_START_PTR(src) + n,
156 GRPC_SLICE_LENGTH(src) - n);
157 }
158
159 static half* other_half(half* h) {
160 if (h == &h->parent->client) return &h->parent->server;
161 return &h->parent->client;
162 }
163
164 static void do_pending_write_op_locked(half* m, grpc_error_handle error) {
165 GPR_ASSERT(m->pending_write_op.is_armed);
166 GPR_ASSERT(m->bytes_written_so_far <=
167 m->parent->channel_effects->allowed_write_bytes);
168 if (m->parent->shutdown) {
169 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->pending_write_op.cb,
170 GRPC_ERROR_CREATE("Already shutdown"));
171 m->pending_write_op.is_armed = false;
172 grpc_slice_buffer_reset_and_unref(m->pending_write_op.slices);
173 return;
174 }
175 if (m->bytes_written_so_far ==
176 m->parent->channel_effects->allowed_write_bytes) {
177 // Keep it in pending state.
178 return;
179 }
180
181 half* other = other_half(m);
182 uint64_t max_writable =
183 std::min<uint64_t>(m->pending_write_op.slices->length,
184 m->parent->channel_effects->allowed_write_bytes -
185 m->bytes_written_so_far);
186 uint64_t max_readable = other->parent->channel_effects->allowed_read_bytes -
187 other->bytes_read_so_far;
188 uint64_t immediate_bytes_read =
189 other->on_read != nullptr ? std::min<uint64_t>(max_readable, max_writable)
190 : 0;
191
192 GPR_ASSERT(max_writable > 0);
193 GPR_ASSERT(max_readable >= 0);
194 // At the end of this process, we should have written max_writable bytes;
195 if (m->parent->simulate_channel_actions) {
196 m->bytes_written_so_far += max_writable;
197 }
198 // Estimate if the original write would still be pending at the end of this
199 // process
200 bool would_write_be_pending =
201 max_writable < m->pending_write_op.slices->length;
202 if (!m->parent->simulate_channel_actions) {
203 GPR_ASSERT(!would_write_be_pending);
204 }
205 grpc_slice_buffer* slices = m->pending_write_op.slices;
206 grpc_slice_buffer* dest =
207 other->on_read != nullptr ? other->on_read_out : &other->read_buffer;
208 while (max_writable > 0) {
209 grpc_slice slice = grpc_slice_buffer_take_first(slices);
210 uint64_t slice_length = GPR_SLICE_LENGTH(slice);
211 GPR_ASSERT(slice_length > 0);
212 grpc_slice split1, split2;
213 uint64_t split_length = 0;
214 if (slice_length <= max_readable) {
215 split_length = std::min<uint64_t>(slice_length, max_writable);
216 } else if (max_readable > 0) {
217 // slice_length > max_readable
218 split_length = std::min<uint64_t>(max_readable, max_writable);
219 } else {
220 // slice_length still > max_readable but max_readable is 0.
221 // In this case put the bytes into other->read_buffer. During a future
222 // read if max_readable still remains zero at the time of read, the
223 // pending read logic will kick in.
224 dest = &other->read_buffer;
225 split_length = std::min<uint64_t>(slice_length, max_writable);
226 }
227
228 grpc_slice_copy_split(slice, split_length, split1, split2);
229 grpc_slice_unref(slice);
230 // Write a copy of the slice to the destination to be read
231 grpc_slice_buffer_add_indexed(dest, split1);
232 // Re-insert split2 into source for next iteration.
233 if (GPR_SLICE_LENGTH(split2) > 0) {
234 grpc_slice_buffer_undo_take_first(slices, split2);
235 } else {
236 grpc_slice_unref(split2);
237 }
238
239 if (max_readable > 0) {
240 GPR_ASSERT(max_readable >= static_cast<uint64_t>(split_length));
241 max_readable -= split_length;
242 }
243
244 GPR_ASSERT(max_writable >= static_cast<uint64_t>(split_length));
245 max_writable -= split_length;
246 }
247
248 if (immediate_bytes_read > 0) {
249 GPR_ASSERT(!other->pending_read_op.is_armed);
250 if (m->parent->simulate_channel_actions) {
251 other->bytes_read_so_far += immediate_bytes_read;
252 }
253 grpc_core::ExecCtx::Run(DEBUG_LOCATION, other->on_read, error);
254 other->on_read = nullptr;
255 }
256
257 if (!would_write_be_pending) {
258 // No slices should be left
259 GPR_ASSERT(m->pending_write_op.slices->count == 0);
260 grpc_slice_buffer_reset_and_unref(m->pending_write_op.slices);
261 m->pending_write_op.is_armed = false;
262 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->pending_write_op.cb, error);
263 }
264 }
265
266 static void me_write(grpc_endpoint* ep, grpc_slice_buffer* slices,
267 grpc_closure* cb, void* /*arg*/, int /*max_frame_size*/) {
268 half* m = reinterpret_cast<half*>(ep);
269 gpr_mu_lock(&m->parent->mu);
270 gpr_atm_no_barrier_fetch_add(&m->parent->stats->num_writes, (gpr_atm)1);
271 if (m->parent->shutdown) {
272 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb,
273 GRPC_ERROR_CREATE("Endpoint already shutdown"));
274 } else {
275 GPR_ASSERT(!m->pending_write_op.is_armed);
276 // Copy slices into m->pending_write_op.slices
277 m->pending_write_op.slices = &m->write_buffer;
278 GPR_ASSERT(m->pending_write_op.slices->count == 0);
279 for (int i = 0; i < static_cast<int>(slices->count); i++) {
280 if (GPR_SLICE_LENGTH(slices->slices[i]) > 0) {
281 grpc_slice_buffer_add_indexed(m->pending_write_op.slices,
282 grpc_slice_copy(slices->slices[i]));
283 }
284 }
285 if (m->pending_write_op.slices->count > 0) {
286 m->pending_write_op.is_armed = true;
287 m->pending_write_op.cb = cb;
288 m->pending_write_op.ep = ep;
289 do_pending_write_op_locked(m, absl::OkStatus());
290 } else {
291 // There is nothing to write. Schedule callback to be run right away.
292 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, absl::OkStatus());
293 }
294 }
295 gpr_mu_unlock(&m->parent->mu);
296 }
297
298 void flush_pending_ops_locked(half* m, grpc_error_handle error) {
299 if (m->pending_read_op.is_armed) {
300 do_pending_read_op_locked(m, error);
301 }
302 if (m->pending_write_op.is_armed) {
303 do_pending_write_op_locked(m, error);
304 }
305 }
306
307 static void me_add_to_pollset(grpc_endpoint* /*ep*/,
308 grpc_pollset* /*pollset*/) {}
309
310 static void me_add_to_pollset_set(grpc_endpoint* /*ep*/,
311 grpc_pollset_set* /*pollset*/) {}
312
313 static void me_delete_from_pollset_set(grpc_endpoint* /*ep*/,
314 grpc_pollset_set* /*pollset*/) {}
315
316 static void shutdown_locked(half* m, grpc_error_handle why) {
317 m->parent->shutdown = true;
318 flush_pending_ops_locked(m, absl::OkStatus());
319 if (m->on_read) {
320 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->on_read,
321 GRPC_ERROR_CREATE_REFERENCING("Shutdown", &why, 1));
322 m->on_read = nullptr;
323 }
324 m = other_half(m);
325 flush_pending_ops_locked(m, absl::OkStatus());
326 if (m->on_read) {
327 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->on_read,
328 GRPC_ERROR_CREATE_REFERENCING("Shutdown", &why, 1));
329 m->on_read = nullptr;
330 }
331 }
332
333 static void me_shutdown(grpc_endpoint* ep, grpc_error_handle why) {
334 half* m = reinterpret_cast<half*>(ep);
335 gpr_mu_lock(&m->parent->mu);
336 shutdown_locked(m, why);
337 gpr_mu_unlock(&m->parent->mu);
338 }
339
340 void grpc_passthru_endpoint_destroy(passthru_endpoint* p) {
341 gpr_mu_destroy(&p->mu);
342 grpc_passthru_endpoint_stats_destroy(p->stats);
343 delete p->channel_effects;
344 grpc_slice_buffer_destroy(&p->client.read_buffer);
345 grpc_slice_buffer_destroy(&p->server.read_buffer);
346 grpc_slice_buffer_destroy(&p->client.write_buffer);
347 grpc_slice_buffer_destroy(&p->server.write_buffer);
348 gpr_free(p);
349 }
350
351 static void me_destroy(grpc_endpoint* ep) {
352 passthru_endpoint* p = (reinterpret_cast<half*>(ep))->parent;
353 gpr_mu_lock(&p->mu);
354 if (0 == --p->halves && p->channel_effects->actions.empty()) {
355 // no pending channel actions exist
356 gpr_mu_unlock(&p->mu);
357 grpc_passthru_endpoint_destroy(p);
358 } else {
359 if (p->halves == 0 && p->simulate_channel_actions) {
360 grpc_timer_cancel(&p->channel_effects->timer);
361 }
362 gpr_mu_unlock(&p->mu);
363 }
364 }
365
366 static absl::string_view me_get_peer(grpc_endpoint* ep) {
367 passthru_endpoint* p = (reinterpret_cast<half*>(ep))->parent;
368 return (reinterpret_cast<half*>(ep)) == &p->client
369 ? "fake:mock_client_endpoint"
370 : "fake:mock_server_endpoint";
371 }
372
373 static absl::string_view me_get_local_address(grpc_endpoint* ep) {
374 passthru_endpoint* p = (reinterpret_cast<half*>(ep))->parent;
375 return (reinterpret_cast<half*>(ep)) == &p->client
376 ? "fake:mock_client_endpoint"
377 : "fake:mock_server_endpoint";
378 }
379
380 static int me_get_fd(grpc_endpoint* /*ep*/) { return -1; }
381
382 static bool me_can_track_err(grpc_endpoint* /*ep*/) { return false; }
383
384 static const grpc_endpoint_vtable vtable = {
385 me_read,
386 me_write,
387 me_add_to_pollset,
388 me_add_to_pollset_set,
389 me_delete_from_pollset_set,
390 me_shutdown,
391 me_destroy,
392 me_get_peer,
393 me_get_local_address,
394 me_get_fd,
395 me_can_track_err,
396 };
397
398 static void half_init(half* m, passthru_endpoint* parent,
399 const char* half_name) {
400 m->base.vtable = &vtable;
401 m->parent = parent;
402 grpc_slice_buffer_init(&m->read_buffer);
403 grpc_slice_buffer_init(&m->write_buffer);
404 m->pending_write_op.slices = nullptr;
405 m->on_read = nullptr;
406 m->bytes_read_so_far = 0;
407 m->bytes_written_so_far = 0;
408 m->pending_write_op.is_armed = false;
409 m->pending_read_op.is_armed = false;
410 std::string name =
411 absl::StrFormat("passthru_endpoint_%s_%p", half_name, parent);
412 }
413
414 void grpc_passthru_endpoint_create(grpc_endpoint** client,
415 grpc_endpoint** server,
416 grpc_passthru_endpoint_stats* stats,
417 bool simulate_channel_actions) {
418 passthru_endpoint* m =
419 static_cast<passthru_endpoint*>(gpr_malloc(sizeof(*m)));
420 m->halves = 2;
421 m->shutdown = false;
422 if (stats == nullptr) {
423 m->stats = grpc_passthru_endpoint_stats_create();
424 } else {
425 gpr_ref(&stats->refs);
426 m->stats = stats;
427 }
428 m->channel_effects = new grpc_passthru_endpoint_channel_effects();
429 m->simulate_channel_actions = simulate_channel_actions;
430 if (!simulate_channel_actions) {
431 m->channel_effects->allowed_read_bytes = UINT64_MAX;
432 m->channel_effects->allowed_write_bytes = UINT64_MAX;
433 }
434 half_init(&m->client, m, "client");
435 half_init(&m->server, m, "server");
436 gpr_mu_init(&m->mu);
437 *client = &m->client.base;
438 *server = &m->server.base;
439 }
440
441 grpc_passthru_endpoint_stats* grpc_passthru_endpoint_stats_create() {
442 grpc_passthru_endpoint_stats* stats =
443 static_cast<grpc_passthru_endpoint_stats*>(
444 gpr_malloc(sizeof(grpc_passthru_endpoint_stats)));
445 memset(stats, 0, sizeof(*stats));
446 gpr_ref_init(&stats->refs, 1);
447 return stats;
448 }
449
450 void grpc_passthru_endpoint_stats_destroy(grpc_passthru_endpoint_stats* stats) {
451 if (gpr_unref(&stats->refs)) {
452 gpr_free(stats);
453 }
454 }
455
456 static void sched_next_channel_action_locked(half* m);
457
458 static void do_next_sched_channel_action(void* arg, grpc_error_handle error) {
459 half* m = reinterpret_cast<half*>(arg);
460 gpr_mu_lock(&m->parent->mu);
461 GPR_ASSERT(!m->parent->channel_effects->actions.empty());
462 if (m->parent->halves == 0) {
463 gpr_mu_unlock(&m->parent->mu);
464 grpc_passthru_endpoint_destroy(m->parent);
465 return;
466 }
467 auto curr_action = m->parent->channel_effects->actions[0];
468 m->parent->channel_effects->actions.erase(
469 m->parent->channel_effects->actions.begin());
470 m->parent->channel_effects->allowed_read_bytes +=
471 curr_action.add_n_readable_bytes;
472 m->parent->channel_effects->allowed_write_bytes +=
473 curr_action.add_n_writable_bytes;
474 flush_pending_ops_locked(m, error);
475 flush_pending_ops_locked(other_half(m), error);
476 sched_next_channel_action_locked(m);
477 gpr_mu_unlock(&m->parent->mu);
478 }
479
480 static void sched_next_channel_action_locked(half* m) {
481 if (m->parent->channel_effects->actions.empty()) {
482 grpc_error_handle err = GRPC_ERROR_CREATE("Channel actions complete");
483 shutdown_locked(m, err);
484 return;
485 }
486 grpc_timer_init(&m->parent->channel_effects->timer,
487 grpc_core::Duration::Milliseconds(
488 m->parent->channel_effects->actions[0].wait_ms) +
489 grpc_core::Timestamp::Now(),
490 GRPC_CLOSURE_CREATE(do_next_sched_channel_action, m,
491 grpc_schedule_on_exec_ctx));
492 }
493
494 void start_scheduling_grpc_passthru_endpoint_channel_effects(
495 grpc_endpoint* ep,
496 const std::vector<grpc_passthru_endpoint_channel_action>& actions) {
497 half* m = reinterpret_cast<half*>(ep);
498 gpr_mu_lock(&m->parent->mu);
499 if (!m->parent->simulate_channel_actions || m->parent->shutdown) {
500 gpr_mu_unlock(&m->parent->mu);
501 return;
502 }
503 m->parent->channel_effects->actions = actions;
504 sched_next_channel_action_locked(m);
505 gpr_mu_unlock(&m->parent->mu);
506 }