| 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/compression/compression_internal.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "absl/container/inlined_vector.h" |
| 27 |
#include "absl/strings/ascii.h" |
| 28 |
#include "absl/strings/str_split.h" |
| 29 |
|
| 30 |
#include <grpc/support/log.h> |
| 31 |
|
| 32 |
#include "src/core/lib/channel/channel_args.h" |
| 33 |
#include "src/core/lib/debug/trace.h" |
| 34 |
#include "src/core/lib/surface/api_trace.h" |
| 35 |
|
| 36 |
namespace grpc_core { |
| 37 |
|
| 38 |
const char* CompressionAlgorithmAsString(grpc_compression_algorithm algorithm) { |
| 39 |
switch (algorithm) { |
| 40 |
case GRPC_COMPRESS_NONE: |
| 41 |
return "identity"; |
| 42 |
case GRPC_COMPRESS_DEFLATE: |
| 43 |
return "deflate"; |
| 44 |
case GRPC_COMPRESS_GZIP: |
| 45 |
return "gzip"; |
| 46 |
case GRPC_COMPRESS_ALGORITHMS_COUNT: |
| 47 |
default: |
| 48 |
return nullptr; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
namespace { |
| 53 |
class CommaSeparatedLists { |
| 54 |
public: |
| 55 |
CommaSeparatedLists() : lists_{}, text_buffer_{} { |
| 56 |
char* text_buffer = text_buffer_; |
| 57 |
auto add_char = [&text_buffer, this](char c) { |
| 58 |
if (text_buffer - text_buffer_ == kTextBufferSize) abort(); |
| 59 |
*text_buffer++ = c; |
| 60 |
}; |
| 61 |
for (size_t list = 0; list < kNumLists; ++list) { |
| 62 |
char* start = text_buffer; |
| 63 |
for (size_t algorithm = 0; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 64 |
++algorithm) { |
| 65 |
if ((list & (1 << algorithm)) == 0) continue; |
| 66 |
if (start != text_buffer) { |
| 67 |
add_char(','); |
| 68 |
add_char(' '); |
| 69 |
} |
| 70 |
const char* name = CompressionAlgorithmAsString( |
| 71 |
static_cast<grpc_compression_algorithm>(algorithm)); |
| 72 |
for (const char* p = name; *p != '\0'; ++p) { |
| 73 |
add_char(*p); |
| 74 |
} |
| 75 |
} |
| 76 |
lists_[list] = absl::string_view(start, text_buffer - start); |
| 77 |
} |
| 78 |
if (text_buffer - text_buffer_ != kTextBufferSize) abort(); |
| 79 |
} |
| 80 |
|
| 81 |
absl::string_view operator[](size_t list) const { return lists_[list]; } |
| 82 |
|
| 83 |
private: |
| 84 |
static constexpr size_t kNumLists = 1 << GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 85 |
// Experimentally determined (tweak things until it runs). |
| 86 |
static constexpr size_t kTextBufferSize = 86; |
| 87 |
absl::string_view lists_[kNumLists]; |
| 88 |
char text_buffer_[kTextBufferSize]; |
| 89 |
}; |
| 90 |
|
| 91 |
const CommaSeparatedLists kCommaSeparatedLists; |
| 92 |
} // namespace |
| 93 |
|
| 94 |
absl::optional<grpc_compression_algorithm> ParseCompressionAlgorithm( |
| 95 |
absl::string_view algorithm) { |
| 96 |
if (algorithm == "identity") { |
| 97 |
return GRPC_COMPRESS_NONE; |
| 98 |
} else if (algorithm == "deflate") { |
| 99 |
return GRPC_COMPRESS_DEFLATE; |
| 100 |
} else if (algorithm == "gzip") { |
| 101 |
return GRPC_COMPRESS_GZIP; |
| 102 |
} else { |
| 103 |
return absl::nullopt; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
grpc_compression_algorithm |
| 108 |
CompressionAlgorithmSet::CompressionAlgorithmForLevel( |
| 109 |
grpc_compression_level level) const { |
| 110 |
GRPC_API_TRACE("grpc_message_compression_algorithm_for_level(level=%d)", 1, |
| 111 |
((int)level)); |
| 112 |
if (level > GRPC_COMPRESS_LEVEL_HIGH) { |
| 113 |
gpr_log(GPR_ERROR, "Unknown message compression level %d.", |
| 114 |
static_cast<int>(level)); |
| 115 |
abort(); |
| 116 |
} |
| 117 |
|
| 118 |
if (level == GRPC_COMPRESS_LEVEL_NONE) { |
| 119 |
return GRPC_COMPRESS_NONE; |
| 120 |
} |
| 121 |
|
| 122 |
GPR_ASSERT(level > 0); |
| 123 |
|
| 124 |
/* Establish a "ranking" or compression algorithms in increasing order of |
| 125 |
* compression. |
| 126 |
* This is simplistic and we will probably want to introduce other dimensions |
| 127 |
* in the future (cpu/memory cost, etc). */ |
| 128 |
absl::InlinedVector<grpc_compression_algorithm, |
| 129 |
GRPC_COMPRESS_ALGORITHMS_COUNT> |
| 130 |
algos; |
| 131 |
for (auto algo : {GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE}) { |
| 132 |
if (set_.is_set(algo)) { |
| 133 |
algos.push_back(algo); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (algos.empty()) { |
| 138 |
return GRPC_COMPRESS_NONE; |
| 139 |
} |
| 140 |
|
| 141 |
switch (level) { |
| 142 |
case GRPC_COMPRESS_LEVEL_NONE: |
| 143 |
abort(); /* should have been handled already */ |
| 144 |
case GRPC_COMPRESS_LEVEL_LOW: |
| 145 |
return algos[0]; |
| 146 |
case GRPC_COMPRESS_LEVEL_MED: |
| 147 |
return algos[algos.size() / 2]; |
| 148 |
case GRPC_COMPRESS_LEVEL_HIGH: |
| 149 |
return algos.back(); |
| 150 |
default: |
| 151 |
abort(); |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
CompressionAlgorithmSet CompressionAlgorithmSet::FromUint32(uint32_t value) { |
| 156 |
CompressionAlgorithmSet set; |
| 157 |
for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { |
| 158 |
if (value & (1u << i)) { |
| 159 |
set.set_.set(i); |
| 160 |
} |
| 161 |
} |
| 162 |
return set; |
| 163 |
} |
| 164 |
|
| 165 |
CompressionAlgorithmSet CompressionAlgorithmSet::FromChannelArgs( |
| 166 |
const grpc_channel_args* args) { |
| 167 |
CompressionAlgorithmSet set; |
| 168 |
static const uint32_t kEverything = |
| 169 |
(1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
| 170 |
if (args != nullptr) { |
| 171 |
set = CompressionAlgorithmSet::FromUint32(grpc_channel_args_find_integer( |
| 172 |
args, GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, |
| 173 |
grpc_integer_options{kEverything, 0, kEverything})); |
| 174 |
set.Set(GRPC_COMPRESS_NONE); |
| 175 |
} else { |
| 176 |
set = CompressionAlgorithmSet::FromUint32(kEverything); |
| 177 |
} |
| 178 |
return set; |
| 179 |
} |
| 180 |
|
| 181 |
CompressionAlgorithmSet::CompressionAlgorithmSet() = default; |
| 182 |
|
| 183 |
CompressionAlgorithmSet::CompressionAlgorithmSet( |
| 184 |
std::initializer_list<grpc_compression_algorithm> algorithms) { |
| 185 |
for (auto algorithm : algorithms) { |
| 186 |
Set(algorithm); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
bool CompressionAlgorithmSet::IsSet( |
| 191 |
grpc_compression_algorithm algorithm) const { |
| 192 |
size_t i = static_cast<size_t>(algorithm); |
| 193 |
if (i < GRPC_COMPRESS_ALGORITHMS_COUNT) { |
| 194 |
return set_.is_set(i); |
| 195 |
} else { |
| 196 |
return false; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void CompressionAlgorithmSet::Set(grpc_compression_algorithm algorithm) { |
| 201 |
size_t i = static_cast<size_t>(algorithm); |
| 202 |
if (i < GRPC_COMPRESS_ALGORITHMS_COUNT) { |
| 203 |
set_.set(i); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
absl::string_view CompressionAlgorithmSet::ToString() const { |
| 208 |
return kCommaSeparatedLists[ToLegacyBitmask()]; |
| 209 |
} |
| 210 |
|
| 211 |
Slice CompressionAlgorithmSet::ToSlice() const { |
| 212 |
return Slice::FromStaticString(ToString()); |
| 213 |
} |
| 214 |
|
| 215 |
CompressionAlgorithmSet CompressionAlgorithmSet::FromString( |
| 216 |
absl::string_view str) { |
| 217 |
CompressionAlgorithmSet set{GRPC_COMPRESS_NONE}; |
| 218 |
for (auto algorithm : absl::StrSplit(str, ',')) { |
| 219 |
auto parsed = |
| 220 |
ParseCompressionAlgorithm(absl::StripAsciiWhitespace(algorithm)); |
| 221 |
if (parsed.has_value()) { |
| 222 |
set.Set(*parsed); |
| 223 |
} |
| 224 |
} |
| 225 |
return set; |
| 226 |
} |
| 227 |
|
| 228 |
uint32_t CompressionAlgorithmSet::ToLegacyBitmask() const { |
| 229 |
return set_.ToInt<uint32_t>(); |
| 230 |
} |
| 231 |
|
| 232 |
absl::optional<grpc_compression_algorithm> |
| 233 |
DefaultCompressionAlgorithmFromChannelArgs(const grpc_channel_args* args) { |
| 234 |
if (args == nullptr) return absl::nullopt; |
| 235 |
for (size_t i = 0; i < args->num_args; i++) { |
| 236 |
if (strcmp(args->args[i].key, GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM) == |
| 237 |
0) { |
| 238 |
if (args->args[i].type == GRPC_ARG_INTEGER) { |
| 239 |
return static_cast<grpc_compression_algorithm>( |
| 240 |
args->args[i].value.integer); |
| 241 |
} else if (args->args[i].type == GRPC_ARG_STRING) { |
| 242 |
return ParseCompressionAlgorithm(args->args[i].value.string); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
return absl::nullopt; |
| 247 |
} |
| 248 |
|
| 249 |
} // namespace grpc_core |