blob: b95ed06f2bafa660c6c39b64b130221e4e4e1004 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, 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
34#include "src/core/channel/connected_channel.h"
35
36#include <stdarg.h>
37#include <stdio.h>
38#include <string.h>
39
Craig Tiller485d7762015-01-23 12:54:05 -080040#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include "src/core/transport/transport.h"
42#include <grpc/byte_buffer.h>
43#include <grpc/support/alloc.h>
44#include <grpc/support/log.h>
45#include <grpc/support/slice_buffer.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
47#define MAX_BUFFER_LENGTH 8192
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Craig Tillerf2350182015-03-02 16:39:39 +000049typedef struct connected_channel_channel_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050 grpc_transport *transport;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051} channel_data;
52
Craig Tiller06aeea72015-04-23 10:54:45 -070053typedef struct connected_channel_call_data { void *unused; } call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
55/* We perform a small hack to locate transport data alongside the connected
56 channel data in call allocations, to allow everything to be pulled in minimal
57 cache line requests */
Yang Gao5fd0d292015-01-26 00:19:48 -080058#define TRANSPORT_STREAM_FROM_CALL_DATA(calld) ((grpc_stream *)((calld) + 1))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059#define CALL_DATA_FROM_TRANSPORT_STREAM(transport_stream) \
60 (((call_data *)(transport_stream)) - 1)
61
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062/* Intercept a call operation and either push it directly up or translate it
63 into transport stream operations */
Craig Tillere039f032015-06-25 12:54:23 -070064static void con_start_transport_stream_op(grpc_call_element *elem,
65 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066 call_data *calld = elem->call_data;
67 channel_data *chand = elem->channel_data;
68 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
69 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
70
Craig Tillere039f032015-06-25 12:54:23 -070071 grpc_transport_perform_stream_op(chand->transport,
72 TRANSPORT_STREAM_FROM_CALL_DATA(calld), op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073}
74
Craig Tillere039f032015-06-25 12:54:23 -070075static void con_start_transport_op(grpc_channel_element *elem,
76 grpc_transport_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077 channel_data *chand = elem->channel_data;
Craig Tillere039f032015-06-25 12:54:23 -070078 grpc_transport_perform_op(chand->transport, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079}
80
81/* Constructor for call_data */
82static void init_call_elem(grpc_call_element *elem,
Craig Tiller3f2c2212015-04-23 07:56:33 -070083 const void *server_transport_data,
Craig Tillerb7959a02015-06-25 08:50:54 -070084 grpc_transport_stream_op *initial_op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085 call_data *calld = elem->call_data;
86 channel_data *chand = elem->channel_data;
87 int r;
88
89 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
Craig Tiller50d9db52015-04-23 10:52:14 -070090 r = grpc_transport_init_stream(chand->transport,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091 TRANSPORT_STREAM_FROM_CALL_DATA(calld),
Craig Tiller3f2c2212015-04-23 07:56:33 -070092 server_transport_data, initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 GPR_ASSERT(r == 0);
94}
95
96/* Destructor for call_data */
97static void destroy_call_elem(grpc_call_element *elem) {
98 call_data *calld = elem->call_data;
99 channel_data *chand = elem->channel_data;
100 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 grpc_transport_destroy_stream(chand->transport,
102 TRANSPORT_STREAM_FROM_CALL_DATA(calld));
103}
104
105/* Constructor for channel_data */
Craig Tiller079a11b2015-06-30 10:07:15 -0700106static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107 const grpc_channel_args *args, grpc_mdctx *mdctx,
108 int is_first, int is_last) {
109 channel_data *cd = (channel_data *)elem->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 GPR_ASSERT(is_last);
111 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
112 cd->transport = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113}
114
115/* Destructor for channel_data */
116static void destroy_channel_elem(grpc_channel_element *elem) {
117 channel_data *cd = (channel_data *)elem->channel_data;
118 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
119 grpc_transport_destroy(cd->transport);
120}
121
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700122static char *con_get_peer(grpc_call_element *elem) {
123 channel_data *chand = elem->channel_data;
124 return grpc_transport_get_peer(chand->transport);
125}
126
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127const grpc_channel_filter grpc_connected_channel_filter = {
Craig Tillere039f032015-06-25 12:54:23 -0700128 con_start_transport_stream_op,
129 con_start_transport_op,
130 sizeof(call_data),
131 init_call_elem,
132 destroy_call_elem,
133 sizeof(channel_data),
134 init_channel_elem,
135 destroy_channel_elem,
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700136 con_get_peer,
Craig Tillere039f032015-06-25 12:54:23 -0700137 "connected",
Craig Tiller87d5b192015-04-16 14:37:57 -0700138};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800139
Craig Tiller1064f8b2015-06-25 13:52:57 -0700140void grpc_connected_channel_bind_transport(grpc_channel_stack *channel_stack,
141 grpc_transport *transport) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142 /* Assumes that the connected channel filter is always the last filter
143 in a channel stack */
144 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
145 channel_data *cd = (channel_data *)elem->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
147 GPR_ASSERT(cd->transport == NULL);
148 cd->transport = transport;
149
150 /* HACK(ctiller): increase call stack size for the channel to make space
151 for channel data. We need a cleaner (but performant) way to do this,
152 and I'm not sure what that is yet.
153 This is only "safe" because call stacks place no additional data after
154 the last call element, and the last call element MUST be the connected
155 channel. */
156 channel_stack->call_stack_size += grpc_transport_stream_size(transport);
Craig Tiller190d3602015-02-18 09:23:38 -0800157}