blob: 14cb3da62da41ea773a60792e262e9b1b0bb7685 [file] [log] [blame]
David Garcia Quintas55b4ea12015-06-16 14:27:32 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070034#include <assert.h>
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070035#include <string.h>
36
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070037#include <grpc/compression.h>
38#include <grpc/support/log.h>
39#include <grpc/support/slice_buffer.h>
40
David Garcia Quintas20afd462015-07-06 23:01:39 -070041#include "src/core/channel/compress_filter.h"
42#include "src/core/channel/channel_args.h"
43#include "src/core/compression/message_compress.h"
44
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070045typedef struct call_data {
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070046 gpr_slice_buffer slices; /**< Buffers up input slices to be compressed */
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070047 grpc_linked_mdelem compression_algorithm_storage;
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070048 int remaining_slice_bytes; /**< Input data to be read, as per BEGIN_MESSAGE */
49 int written_initial_metadata; /**< Already processed initial md? */
50 /** Compression algorithm we'll try to use. It may be given by incoming
51 * metadata, or by the channel's default compression settings. */
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070052 grpc_compression_algorithm compression_algorithm;
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070053 /** If true, contents of \a compression_algorithm are authoritative */
54 int has_compression_algorithm;
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070055} call_data;
56
57typedef struct channel_data {
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070058 /** Metadata key for the incoming (requested) compression algorithm */
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070059 grpc_mdstr *mdstr_request_compression_algorithm_key;
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070060 /** Metadata key for the outgoing (used) compression algorithm */
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070061 grpc_mdstr *mdstr_outgoing_compression_algorithm_key;
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070062 /** Precomputed metadata elements for all available compression algorithms */
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070063 grpc_mdelem *mdelem_compression_algorithms[GRPC_COMPRESS_ALGORITHMS_COUNT];
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -070064 /** The default, channel-level, compression algorithm */
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070065 grpc_compression_algorithm default_compression_algorithm;
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070066} channel_data;
67
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070068/** Compress \a slices in place using \a algorithm. Returns 1 if compression did
69 * actually happen, 0 otherwise (for example if the compressed output size was
70 * larger than the raw input).
71 *
72 * Returns 1 if the data was actually compress and 0 otherwise. */
73static int compress_send_sb(grpc_compression_algorithm algorithm,
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070074 gpr_slice_buffer *slices) {
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070075 int did_compress;
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070076 gpr_slice_buffer tmp;
77 gpr_slice_buffer_init(&tmp);
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070078 did_compress = grpc_msg_compress(algorithm, slices, &tmp);
David Garcia Quintas4e403362015-07-01 16:45:34 -070079 if (did_compress) {
80 gpr_slice_buffer_swap(slices, &tmp);
81 }
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070082 gpr_slice_buffer_destroy(&tmp);
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070083 return did_compress;
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070084}
85
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070086/** For each \a md element from the incoming metadata, filter out the entry for
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070087 * "grpc-encoding", using its value to populate the call data's
David Garcia Quintasd16af0e2015-06-22 22:39:21 -070088 * compression_algorithm field. */
89static grpc_mdelem* compression_md_filter(void *user_data, grpc_mdelem *md) {
90 grpc_call_element *elem = user_data;
91 call_data *calld = elem->call_data;
92 channel_data *channeld = elem->channel_data;
93
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070094 if (md->key == channeld->mdstr_request_compression_algorithm_key) {
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070095 const char *md_c_str = grpc_mdstr_as_c_string(md->value);
96 if (!grpc_compression_algorithm_parse(md_c_str,
97 &calld->compression_algorithm)) {
98 gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'. Ignoring.",
99 md_c_str);
100 calld->compression_algorithm = GRPC_COMPRESS_NONE;
101 }
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700102 calld->has_compression_algorithm = 1;
103 return NULL;
104 }
105
106 return md;
107}
108
109static int skip_compression(channel_data *channeld, call_data *calld) {
David Garcia Quintasfc0fa332015-06-25 18:11:07 -0700110 if (calld->has_compression_algorithm) {
111 if (calld->compression_algorithm == GRPC_COMPRESS_NONE) {
112 return 1;
113 }
114 return 0; /* we have an actual call-specific algorithm */
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700115 }
116 /* no per-call compression override */
117 return channeld->default_compression_algorithm == GRPC_COMPRESS_NONE;
118}
119
David Garcia Quintas658b6082015-07-15 13:46:00 -0700120/** Assembles a new grpc_stream_op_buffer with the compressed slices, modifying
121 * the associated GRPC_OP_BEGIN_MESSAGE accordingly (new compressed length,
122 * flags indicating compression is in effect) and replaces \a send_ops with it.
123 * */
David Garcia Quintasa01f7a42015-07-08 00:16:50 -0700124static void finish_compressed_sopb(grpc_stream_op_buffer *send_ops,
125 grpc_call_element *elem) {
David Garcia Quintas20a35382015-07-10 17:48:22 -0700126 size_t i;
David Garcia Quintas20afd462015-07-06 23:01:39 -0700127 call_data *calld = elem->call_data;
David Garcia Quintas20a35382015-07-10 17:48:22 -0700128 int new_slices_added = 0; /* GPR_FALSE */
David Garcia Quintas658b6082015-07-15 13:46:00 -0700129 grpc_metadata_batch metadata;
130 grpc_stream_op_buffer new_send_ops;
David Garcia Quintas20a35382015-07-10 17:48:22 -0700131 grpc_sopb_init(&new_send_ops);
David Garcia Quintas20afd462015-07-06 23:01:39 -0700132
David Garcia Quintas20a35382015-07-10 17:48:22 -0700133 for (i = 0; i < send_ops->nops; i++) {
David Garcia Quintas20afd462015-07-06 23:01:39 -0700134 grpc_stream_op *sop = &send_ops->ops[i];
135 switch (sop->type) {
136 case GRPC_OP_BEGIN_MESSAGE:
David Garcia Quintas20a35382015-07-10 17:48:22 -0700137 grpc_sopb_add_begin_message(
138 &new_send_ops, calld->slices.length,
139 sop->data.begin_message.flags | GRPC_WRITE_INTERNAL_COMPRESS);
David Garcia Quintas20afd462015-07-06 23:01:39 -0700140 break;
David Garcia Quintas20afd462015-07-06 23:01:39 -0700141 case GRPC_OP_SLICE:
David Garcia Quintas658b6082015-07-15 13:46:00 -0700142 /* Once we reach the slices section of the original buffer, simply add
143 * all the new (compressed) slices. We obviously want to do this only
144 * once, hence the "new_slices_added" guard. */
David Garcia Quintas20a35382015-07-10 17:48:22 -0700145 if (!new_slices_added) {
146 size_t j;
147 for (j = 0; j < calld->slices.count; ++j) {
148 grpc_sopb_add_slice(&new_send_ops,
149 gpr_slice_ref(calld->slices.slices[j]));
150 }
151 new_slices_added = 1; /* GPR_TRUE */
David Garcia Quintas20afd462015-07-06 23:01:39 -0700152 }
David Garcia Quintasa01f7a42015-07-08 00:16:50 -0700153 break;
David Garcia Quintasd317e752015-07-15 00:09:27 -0700154 case GRPC_OP_METADATA:
David Garcia Quintas658b6082015-07-15 13:46:00 -0700155 /* move the metadata to the new buffer. */
156 grpc_metadata_batch_move(&metadata, &sop->data.metadata);
157 grpc_sopb_add_metadata(&new_send_ops, metadata);
David Garcia Quintasd317e752015-07-15 00:09:27 -0700158 break;
David Garcia Quintas20afd462015-07-06 23:01:39 -0700159 case GRPC_NO_OP:
David Garcia Quintasa01f7a42015-07-08 00:16:50 -0700160 break;
David Garcia Quintas20afd462015-07-06 23:01:39 -0700161 }
162 }
David Garcia Quintas20a35382015-07-10 17:48:22 -0700163 grpc_sopb_swap(send_ops, &new_send_ops);
164 grpc_sopb_destroy(&new_send_ops);
David Garcia Quintas20afd462015-07-06 23:01:39 -0700165}
166
David Garcia Quintas7c4fdb52015-07-17 14:21:15 -0700167/** Filter's "main" function, called for any incoming grpc_transport_stream_op
168 * instance that holds a non-zero number of send operations, accesible to this
169 * function in \a send_ops. */
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700170static void process_send_ops(grpc_call_element *elem,
171 grpc_stream_op_buffer *send_ops) {
172 call_data *calld = elem->call_data;
173 channel_data *channeld = elem->channel_data;
David Garcia Quintas20afd462015-07-06 23:01:39 -0700174 size_t i;
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700175 int did_compress = 0;
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700176
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700177 for (i = 0; i < send_ops->nops; ++i) {
178 grpc_stream_op *sop = &send_ops->ops[i];
179 switch (sop->type) {
180 case GRPC_OP_BEGIN_MESSAGE:
David Garcia Quintas658b6082015-07-15 13:46:00 -0700181 /* buffer up slices until we've processed all the expected ones (as
182 * given by GRPC_OP_BEGIN_MESSAGE) */
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700183 calld->remaining_slice_bytes = sop->data.begin_message.length;
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700184 if (sop->data.begin_message.flags & GRPC_WRITE_NO_COMPRESS) {
185 calld->has_compression_algorithm = 1; /* GPR_TRUE */
186 calld->compression_algorithm = GRPC_COMPRESS_NONE;
187 }
188 break;
189 case GRPC_OP_METADATA:
David Garcia Quintasd317e752015-07-15 00:09:27 -0700190 if (!calld->written_initial_metadata) {
191 /* Parse incoming request for compression. If any, it'll be available
192 * at calld->compression_algorithm */
193 grpc_metadata_batch_filter(&(sop->data.metadata),
194 compression_md_filter, elem);
195 if (!calld->has_compression_algorithm) {
196 /* If no algorithm was found in the metadata and we aren't
197 * exceptionally skipping compression, fall back to the channel
198 * default */
199 calld->compression_algorithm =
200 channeld->default_compression_algorithm;
201 calld->has_compression_algorithm = 1; /* GPR_TRUE */
202 }
203 grpc_metadata_batch_add_head(
204 &(sop->data.metadata), &calld->compression_algorithm_storage,
205 grpc_mdelem_ref(channeld->mdelem_compression_algorithms
206 [calld->compression_algorithm]));
207 calld->written_initial_metadata = 1; /* GPR_TRUE */
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700208 }
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700209 break;
210 case GRPC_OP_SLICE:
David Garcia Quintasd317e752015-07-15 00:09:27 -0700211 if (skip_compression(channeld, calld)) continue;
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700212 GPR_ASSERT(calld->remaining_slice_bytes > 0);
David Garcia Quintas658b6082015-07-15 13:46:00 -0700213 /* Increase input ref count, gpr_slice_buffer_add takes ownership. */
David Garcia Quintas4e403362015-07-01 16:45:34 -0700214 gpr_slice_buffer_add(&calld->slices, gpr_slice_ref(sop->data.slice));
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700215 calld->remaining_slice_bytes -= GPR_SLICE_LENGTH(sop->data.slice);
216 if (calld->remaining_slice_bytes == 0) {
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700217 did_compress =
218 compress_send_sb(calld->compression_algorithm, &calld->slices);
219 }
220 break;
221 case GRPC_NO_OP:
David Garcia Quintasa21e2c82015-07-10 00:15:36 -0700222 break;
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700223 }
224 }
225
David Garcia Quintas20afd462015-07-06 23:01:39 -0700226 /* Modify the send_ops stream_op_buffer depending on whether compression was
227 * carried out */
David Garcia Quintasa01f7a42015-07-08 00:16:50 -0700228 if (did_compress) {
229 finish_compressed_sopb(send_ops, elem);
David Garcia Quintasa01f7a42015-07-08 00:16:50 -0700230 }
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700231}
232
233/* Called either:
234 - in response to an API call (or similar) from above, to send something
235 - a network event (or similar) from below, to receive something
236 op contains type and call direction information, in addition to the data
237 that is being sent or received. */
David Garcia Quintas17bb6492015-07-08 15:16:22 -0700238static void compress_start_transport_stream_op(grpc_call_element *elem,
239 grpc_transport_stream_op *op) {
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700240 if (op->send_ops && op->send_ops->nops > 0) {
241 process_send_ops(elem, op->send_ops);
242 }
243
244 /* pass control down the stack */
245 grpc_call_next_op(elem, op);
246}
247
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700248/* Constructor for call_data */
249static void init_call_elem(grpc_call_element *elem,
250 const void *server_transport_data,
David Garcia Quintas17bb6492015-07-08 15:16:22 -0700251 grpc_transport_stream_op *initial_op) {
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700252 /* grab pointers to our data from the call element */
253 call_data *calld = elem->call_data;
254
255 /* initialize members */
256 gpr_slice_buffer_init(&calld->slices);
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700257 calld->has_compression_algorithm = 0;
David Garcia Quintasd317e752015-07-15 00:09:27 -0700258 calld->written_initial_metadata = 0; /* GPR_FALSE */
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700259
260 if (initial_op) {
261 if (initial_op->send_ops && initial_op->send_ops->nops > 0) {
262 process_send_ops(elem, initial_op->send_ops);
263 }
264 }
265}
266
267/* Destructor for call_data */
268static void destroy_call_elem(grpc_call_element *elem) {
269 /* grab pointers to our data from the call element */
270 call_data *calld = elem->call_data;
271 gpr_slice_buffer_destroy(&calld->slices);
272}
273
274/* Constructor for channel_data */
David Garcia Quintas17bb6492015-07-08 15:16:22 -0700275static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master,
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700276 const grpc_channel_args *args, grpc_mdctx *mdctx,
277 int is_first, int is_last) {
278 channel_data *channeld = elem->channel_data;
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700279 grpc_compression_algorithm algo_idx;
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700280
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700281 channeld->default_compression_algorithm =
David Garcia Quintascadbf222015-07-17 15:33:13 -0700282 grpc_channel_args_get_compression_algorithm(args);
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700283
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700284 channeld->mdstr_request_compression_algorithm_key =
285 grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY);
286
287 channeld->mdstr_outgoing_compression_algorithm_key =
David Garcia Quintasfc0fa332015-06-25 18:11:07 -0700288 grpc_mdstr_from_string(mdctx, "grpc-encoding");
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700289
290 for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) {
David Garcia Quintasfc0fa332015-06-25 18:11:07 -0700291 char *algorith_name;
292 GPR_ASSERT(grpc_compression_algorithm_name(algo_idx, &algorith_name) != 0);
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700293 channeld->mdelem_compression_algorithms[algo_idx] =
294 grpc_mdelem_from_metadata_strings(
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700295 mdctx,
296 grpc_mdstr_ref(channeld->mdstr_outgoing_compression_algorithm_key),
David Garcia Quintasfc0fa332015-06-25 18:11:07 -0700297 grpc_mdstr_from_string(mdctx, algorith_name));
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700298 }
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700299
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700300 GPR_ASSERT(!is_last);
301}
302
303/* Destructor for channel data */
304static void destroy_channel_elem(grpc_channel_element *elem) {
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700305 channel_data *channeld = elem->channel_data;
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700306 grpc_compression_algorithm algo_idx;
307
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700308 grpc_mdstr_unref(channeld->mdstr_request_compression_algorithm_key);
309 grpc_mdstr_unref(channeld->mdstr_outgoing_compression_algorithm_key);
David Garcia Quintasd16af0e2015-06-22 22:39:21 -0700310 for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT;
311 ++algo_idx) {
312 grpc_mdelem_unref(channeld->mdelem_compression_algorithms[algo_idx]);
313 }
David Garcia Quintas55b4ea12015-06-16 14:27:32 -0700314}
315
David Garcia Quintasd317e752015-07-15 00:09:27 -0700316const grpc_channel_filter grpc_compress_filter = {
317 compress_start_transport_stream_op,
318 grpc_channel_next_op,
319 sizeof(call_data),
320 init_call_elem,
321 destroy_call_elem,
322 sizeof(channel_data),
323 init_channel_elem,
324 destroy_channel_elem,
325 "compress"};