3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/ext/transport/chttp2/transport/frame_ping.h"
25 #include "absl/strings/str_format.h"
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 #include "src/core/ext/transport/chttp2/transport/internal.h"
32 static bool g_disable_ping_ack = false;
34 grpc_slice grpc_chttp2_ping_create(uint8_t ack, uint64_t opaque_8bytes) {
35 grpc_slice slice = GRPC_SLICE_MALLOC(9 + 8);
36 uint8_t* p = GRPC_SLICE_START_PTR(slice);
41 *p++ = GRPC_CHTTP2_FRAME_PING;
47 *p++ = static_cast<uint8_t>(opaque_8bytes >> 56);
48 *p++ = static_cast<uint8_t>(opaque_8bytes >> 48);
49 *p++ = static_cast<uint8_t>(opaque_8bytes >> 40);
50 *p++ = static_cast<uint8_t>(opaque_8bytes >> 32);
51 *p++ = static_cast<uint8_t>(opaque_8bytes >> 24);
52 *p++ = static_cast<uint8_t>(opaque_8bytes >> 16);
53 *p++ = static_cast<uint8_t>(opaque_8bytes >> 8);
54 *p++ = static_cast<uint8_t>(opaque_8bytes);
59 grpc_error_handle grpc_chttp2_ping_parser_begin_frame(
60 grpc_chttp2_ping_parser* parser, uint32_t length, uint8_t flags) {
61 if (flags & 0xfe || length != 8) {
62 return GRPC_ERROR_CREATE_FROM_CPP_STRING(
63 absl::StrFormat("invalid ping: length=%d, flags=%02x", length, flags));
66 parser->is_ack = flags;
67 parser->opaque_8bytes = 0;
68 return GRPC_ERROR_NONE;
71 grpc_error_handle grpc_chttp2_ping_parser_parse(void* parser,
72 grpc_chttp2_transport* t,
73 grpc_chttp2_stream* /*s*/,
74 const grpc_slice& slice,
76 const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
77 const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
78 const uint8_t* cur = beg;
79 grpc_chttp2_ping_parser* p = static_cast<grpc_chttp2_ping_parser*>(parser);
81 while (p->byte != 8 && cur != end) {
82 p->opaque_8bytes |= ((static_cast<uint64_t>(*cur)) << (56 - 8 * p->byte));
90 grpc_chttp2_ack_ping(t, p->opaque_8bytes);
93 grpc_millis now = grpc_core::ExecCtx::Get()->Now();
94 grpc_millis next_allowed_ping =
95 t->ping_recv_state.last_ping_recv_time +
96 t->ping_policy.min_recv_ping_interval_without_data;
98 if (t->keepalive_permit_without_calls == 0 &&
99 grpc_chttp2_stream_map_size(&t->stream_map) == 0) {
100 /* According to RFC1122, the interval of TCP Keep-Alive is default to
101 no less than two hours. When there is no outstanding streams, we
102 restrict the number of PINGS equivalent to TCP Keep-Alive. */
104 t->ping_recv_state.last_ping_recv_time + 7200 * GPR_MS_PER_SEC;
107 if (next_allowed_ping > now) {
108 grpc_chttp2_add_ping_strike(t);
111 t->ping_recv_state.last_ping_recv_time = now;
113 if (!g_disable_ping_ack) {
114 if (t->ping_ack_count == t->ping_ack_capacity) {
115 t->ping_ack_capacity = GPR_MAX(t->ping_ack_capacity * 3 / 2, 3);
116 t->ping_acks = static_cast<uint64_t*>(gpr_realloc(
117 t->ping_acks, t->ping_ack_capacity * sizeof(*t->ping_acks)));
119 t->num_pending_induced_frames++;
120 t->ping_acks[t->ping_ack_count++] = p->opaque_8bytes;
121 grpc_chttp2_initiate_write(t, GRPC_CHTTP2_INITIATE_WRITE_PING_RESPONSE);
126 return GRPC_ERROR_NONE;
129 void grpc_set_disable_ping_ack(bool disable_ping_ack) {
130 g_disable_ping_ack = disable_ping_ack;