| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 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 <stdint.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <grpc/grpc.h> |
| 23 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 24 |
#include <grpc/slice.h> |
| 25 |
#include <grpc/status.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
#include "src/core/lib/channel/channel_args.h" |
| 29 |
#include "src/core/lib/gpr/useful.h" |
| 30 |
#include "src/core/lib/surface/channel.h" |
| 31 |
#include "test/core/end2end/cq_verifier.h" |
| 32 |
#include "test/core/end2end/end2end_tests.h" |
| 33 |
#include "test/core/util/test_config.h" |
| 34 |
|
| 35 |
#define MAX_PING_STRIKES 2 |
| 36 |
|
| 37 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 38 |
|
| 39 |
static void drain_cq(grpc_completion_queue* cq) { |
| 40 |
grpc_event ev; |
| 41 |
do { |
| 42 |
ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), |
| 43 |
nullptr); |
| 44 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 45 |
} |
| 46 |
|
| 47 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 48 |
if (!f->server) return; |
| 49 |
grpc_server_destroy(f->server); |
| 50 |
f->server = nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 54 |
if (!f->client) return; |
| 55 |
grpc_channel_destroy(f->client); |
| 56 |
f->client = nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 60 |
shutdown_server(f); |
| 61 |
shutdown_client(f); |
| 62 |
|
| 63 |
grpc_completion_queue_shutdown(f->cq); |
| 64 |
drain_cq(f->cq); |
| 65 |
grpc_completion_queue_destroy(f->cq); |
| 66 |
} |
| 67 |
|
| 68 |
// Send more pings than server allows to trigger server's GOAWAY. |
| 69 |
static void test_bad_ping(grpc_end2end_test_config config) { |
| 70 |
grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr); |
| 71 |
grpc_core::CqVerifier cqv(f.cq); |
| 72 |
grpc_arg client_a[] = { |
| 73 |
grpc_channel_arg_integer_create( |
| 74 |
const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0), |
| 75 |
grpc_channel_arg_integer_create( |
| 76 |
const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0)}; |
| 77 |
grpc_arg server_a[] = { |
| 78 |
grpc_channel_arg_integer_create( |
| 79 |
const_cast<char*>( |
| 80 |
GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS), |
| 81 |
300000 /* 5 minutes */), |
| 82 |
grpc_channel_arg_integer_create( |
| 83 |
const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES), MAX_PING_STRIKES), |
| 84 |
grpc_channel_arg_integer_create( |
| 85 |
const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0)}; |
| 86 |
grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; |
| 87 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a}; |
| 88 |
|
| 89 |
config.init_client(&f, &client_args); |
| 90 |
config.init_server(&f, &server_args); |
| 91 |
|
| 92 |
grpc_call* c; |
| 93 |
grpc_call* s; |
| 94 |
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10); |
| 95 |
grpc_op ops[6]; |
| 96 |
grpc_op* op; |
| 97 |
grpc_metadata_array initial_metadata_recv; |
| 98 |
grpc_metadata_array trailing_metadata_recv; |
| 99 |
grpc_metadata_array request_metadata_recv; |
| 100 |
grpc_call_details call_details; |
| 101 |
grpc_status_code status; |
| 102 |
grpc_call_error error; |
| 103 |
grpc_slice details; |
| 104 |
int was_cancelled = 2; |
| 105 |
|
| 106 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 107 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 108 |
deadline, nullptr); |
| 109 |
GPR_ASSERT(c); |
| 110 |
|
| 111 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 112 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 113 |
grpc_metadata_array_init(&request_metadata_recv); |
| 114 |
grpc_call_details_init(&call_details); |
| 115 |
|
| 116 |
memset(ops, 0, sizeof(ops)); |
| 117 |
op = ops; |
| 118 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 119 |
op->data.send_initial_metadata.count = 0; |
| 120 |
op->data.send_initial_metadata.metadata = nullptr; |
| 121 |
op->flags = 0; |
| 122 |
op->reserved = nullptr; |
| 123 |
op++; |
| 124 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 125 |
op->flags = 0; |
| 126 |
op->reserved = nullptr; |
| 127 |
op++; |
| 128 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 129 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 130 |
op->flags = 0; |
| 131 |
op->reserved = nullptr; |
| 132 |
op++; |
| 133 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 134 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 135 |
op->data.recv_status_on_client.status = &status; |
| 136 |
op->data.recv_status_on_client.status_details = &details; |
| 137 |
op->flags = 0; |
| 138 |
op->reserved = nullptr; |
| 139 |
op++; |
| 140 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 141 |
nullptr); |
| 142 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 143 |
|
| 144 |
error = |
| 145 |
grpc_server_request_call(f.server, &s, &call_details, |
| 146 |
&request_metadata_recv, f.cq, f.cq, tag(101)); |
| 147 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 148 |
cqv.Expect(tag(101), true); |
| 149 |
cqv.Verify(); |
| 150 |
|
| 151 |
// Send too many pings to the server to trigger the punishment: |
| 152 |
// The first ping will let server mark its last_recv time. Afterwards, each |
| 153 |
// ping will trigger a ping strike, and we need at least MAX_PING_STRIKES |
| 154 |
// strikes to trigger the punishment. So (MAX_PING_STRIKES + 2) pings are |
| 155 |
// needed here. |
| 156 |
int i; |
| 157 |
for (i = 1; i <= MAX_PING_STRIKES + 2; i++) { |
| 158 |
grpc_channel_ping(f.client, f.cq, tag(200 + i), nullptr); |
| 159 |
cqv.Expect(tag(200 + i), true); |
| 160 |
if (i == MAX_PING_STRIKES + 2) { |
| 161 |
cqv.Expect(tag(1), true); |
| 162 |
} |
| 163 |
cqv.Verify(); |
| 164 |
} |
| 165 |
|
| 166 |
memset(ops, 0, sizeof(ops)); |
| 167 |
op = ops; |
| 168 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 169 |
op->data.send_initial_metadata.count = 0; |
| 170 |
op->flags = 0; |
| 171 |
op->reserved = nullptr; |
| 172 |
op++; |
| 173 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 174 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 175 |
op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 176 |
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 177 |
op->data.send_status_from_server.status_details = &status_details; |
| 178 |
op->flags = 0; |
| 179 |
op->reserved = nullptr; |
| 180 |
op++; |
| 181 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 182 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 183 |
op->flags = 0; |
| 184 |
op->reserved = nullptr; |
| 185 |
op++; |
| 186 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102), |
| 187 |
nullptr); |
| 188 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 189 |
|
| 190 |
cqv.Expect(tag(102), true); |
| 191 |
cqv.Verify(); |
| 192 |
|
| 193 |
grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); |
| 194 |
cqv.Expect(tag(0xdead), true); |
| 195 |
cqv.Verify(); |
| 196 |
|
| 197 |
grpc_call_unref(s); |
| 198 |
|
| 199 |
// The connection should be closed immediately after the misbehaved pings, |
| 200 |
// the in-progress RPC should fail. |
| 201 |
GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 202 |
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
| 203 |
GPR_ASSERT(was_cancelled == 1); |
| 204 |
|
| 205 |
grpc_slice_unref(details); |
| 206 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 207 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 208 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 209 |
grpc_call_details_destroy(&call_details); |
| 210 |
grpc_call_unref(c); |
| 211 |
end_test(&f); |
| 212 |
config.tear_down_data(&f); |
| 213 |
} |
| 214 |
|
| 215 |
// Try sending more pings than server allows, but server should be fine because |
| 216 |
// max_pings_without_data should limit pings sent out on wire. |
| 217 |
static void test_pings_without_data(grpc_end2end_test_config config) { |
| 218 |
grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr); |
| 219 |
grpc_core::CqVerifier cqv(f.cq); |
| 220 |
// Only allow MAX_PING_STRIKES pings without data (DATA/HEADERS/WINDOW_UPDATE) |
| 221 |
// so that the transport will throttle the excess pings. |
| 222 |
grpc_arg client_a[] = { |
| 223 |
grpc_channel_arg_integer_create( |
| 224 |
const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), |
| 225 |
MAX_PING_STRIKES), |
| 226 |
grpc_channel_arg_integer_create( |
| 227 |
const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0)}; |
| 228 |
grpc_arg server_a[] = { |
| 229 |
grpc_channel_arg_integer_create( |
| 230 |
const_cast<char*>( |
| 231 |
GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS), |
| 232 |
300000 /* 5 minutes */), |
| 233 |
grpc_channel_arg_integer_create( |
| 234 |
const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES), MAX_PING_STRIKES), |
| 235 |
grpc_channel_arg_integer_create( |
| 236 |
const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0)}; |
| 237 |
grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; |
| 238 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a}; |
| 239 |
|
| 240 |
config.init_client(&f, &client_args); |
| 241 |
config.init_server(&f, &server_args); |
| 242 |
|
| 243 |
grpc_call* c; |
| 244 |
grpc_call* s; |
| 245 |
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10); |
| 246 |
grpc_op ops[6]; |
| 247 |
grpc_op* op; |
| 248 |
grpc_metadata_array initial_metadata_recv; |
| 249 |
grpc_metadata_array trailing_metadata_recv; |
| 250 |
grpc_metadata_array request_metadata_recv; |
| 251 |
grpc_call_details call_details; |
| 252 |
grpc_status_code status; |
| 253 |
grpc_call_error error; |
| 254 |
grpc_slice details; |
| 255 |
int was_cancelled = 2; |
| 256 |
|
| 257 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 258 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 259 |
deadline, nullptr); |
| 260 |
GPR_ASSERT(c); |
| 261 |
|
| 262 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 263 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 264 |
grpc_metadata_array_init(&request_metadata_recv); |
| 265 |
grpc_call_details_init(&call_details); |
| 266 |
|
| 267 |
memset(ops, 0, sizeof(ops)); |
| 268 |
op = ops; |
| 269 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 270 |
op->data.send_initial_metadata.count = 0; |
| 271 |
op->data.send_initial_metadata.metadata = nullptr; |
| 272 |
op->flags = 0; |
| 273 |
op->reserved = nullptr; |
| 274 |
op++; |
| 275 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 276 |
op->flags = 0; |
| 277 |
op->reserved = nullptr; |
| 278 |
op++; |
| 279 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 280 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 281 |
op->flags = 0; |
| 282 |
op->reserved = nullptr; |
| 283 |
op++; |
| 284 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 285 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 286 |
op->data.recv_status_on_client.status = &status; |
| 287 |
op->data.recv_status_on_client.status_details = &details; |
| 288 |
op->flags = 0; |
| 289 |
op->reserved = nullptr; |
| 290 |
op++; |
| 291 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 292 |
nullptr); |
| 293 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 294 |
|
| 295 |
error = |
| 296 |
grpc_server_request_call(f.server, &s, &call_details, |
| 297 |
&request_metadata_recv, f.cq, f.cq, tag(101)); |
| 298 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 299 |
cqv.Expect(tag(101), true); |
| 300 |
cqv.Verify(); |
| 301 |
|
| 302 |
// Send too many pings to the server similar to the previous test case. |
| 303 |
// However, since we set the MAX_PINGS_WITHOUT_DATA at the client side, only |
| 304 |
// MAX_PING_STRIKES will actually be sent and the rpc will still succeed. |
| 305 |
int i; |
| 306 |
for (i = 1; i <= MAX_PING_STRIKES + 2; i++) { |
| 307 |
grpc_channel_ping(f.client, f.cq, tag(200 + i), nullptr); |
| 308 |
if (i <= MAX_PING_STRIKES) { |
| 309 |
cqv.Expect(tag(200 + i), true); |
| 310 |
} |
| 311 |
cqv.Verify(); |
| 312 |
} |
| 313 |
|
| 314 |
memset(ops, 0, sizeof(ops)); |
| 315 |
op = ops; |
| 316 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 317 |
op->data.send_initial_metadata.count = 0; |
| 318 |
op->flags = 0; |
| 319 |
op->reserved = nullptr; |
| 320 |
op++; |
| 321 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 322 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 323 |
op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 324 |
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 325 |
op->data.send_status_from_server.status_details = &status_details; |
| 326 |
op->flags = 0; |
| 327 |
op->reserved = nullptr; |
| 328 |
op++; |
| 329 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 330 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 331 |
op->flags = 0; |
| 332 |
op->reserved = nullptr; |
| 333 |
op++; |
| 334 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102), |
| 335 |
nullptr); |
| 336 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 337 |
|
| 338 |
cqv.Expect(tag(102), true); |
| 339 |
// Client call should return. |
| 340 |
cqv.Expect(tag(1), true); |
| 341 |
cqv.Verify(); |
| 342 |
|
| 343 |
grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); |
| 344 |
cqv.Expect(tag(0xdead), true); |
| 345 |
|
| 346 |
// Also expect the previously blocked pings to complete with an error |
| 347 |
cqv.Expect(tag(200 + MAX_PING_STRIKES + 1), false); |
| 348 |
cqv.Expect(tag(200 + MAX_PING_STRIKES + 2), false); |
| 349 |
|
| 350 |
cqv.Verify(); |
| 351 |
|
| 352 |
grpc_call_unref(s); |
| 353 |
|
| 354 |
// The rpc should be successful. |
| 355 |
GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
| 356 |
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
| 357 |
|
| 358 |
grpc_slice_unref(details); |
| 359 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 360 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 361 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 362 |
grpc_call_details_destroy(&call_details); |
| 363 |
grpc_call_unref(c); |
| 364 |
end_test(&f); |
| 365 |
config.tear_down_data(&f); |
| 366 |
} |
| 367 |
|
| 368 |
void bad_ping(grpc_end2end_test_config config) { |
| 369 |
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); |
| 370 |
test_bad_ping(config); |
| 371 |
test_pings_without_data(config); |
| 372 |
} |
| 373 |
|
| 374 |
void bad_ping_pre_init(void) {} |