Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / ext / transport / chttp2 / transport / frame_ping.cc
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/ext/transport/chttp2/transport/frame_ping.h"
22
23 #include <string.h>
24
25 #include "absl/strings/str_format.h"
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29
30 #include "src/core/ext/transport/chttp2/transport/internal.h"
31
32 static bool g_disable_ping_ack = false;
33
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);
37
38   *p++ = 0;
39   *p++ = 0;
40   *p++ = 8;
41   *p++ = GRPC_CHTTP2_FRAME_PING;
42   *p++ = ack ? 1 : 0;
43   *p++ = 0;
44   *p++ = 0;
45   *p++ = 0;
46   *p++ = 0;
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);
55
56   return slice;
57 }
58
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));
64   }
65   parser->byte = 0;
66   parser->is_ack = flags;
67   parser->opaque_8bytes = 0;
68   return GRPC_ERROR_NONE;
69 }
70
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,
75                                                 int is_last) {
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);
80
81   while (p->byte != 8 && cur != end) {
82     p->opaque_8bytes |= ((static_cast<uint64_t>(*cur)) << (56 - 8 * p->byte));
83     cur++;
84     p->byte++;
85   }
86
87   if (p->byte == 8) {
88     GPR_ASSERT(is_last);
89     if (p->is_ack) {
90       grpc_chttp2_ack_ping(t, p->opaque_8bytes);
91     } else {
92       if (!t->is_client) {
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;
97
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. */
103           next_allowed_ping =
104               t->ping_recv_state.last_ping_recv_time + 7200 * GPR_MS_PER_SEC;
105         }
106
107         if (next_allowed_ping > now) {
108           grpc_chttp2_add_ping_strike(t);
109         }
110
111         t->ping_recv_state.last_ping_recv_time = now;
112       }
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)));
118         }
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);
122       }
123     }
124   }
125
126   return GRPC_ERROR_NONE;
127 }
128
129 void grpc_set_disable_ping_ack(bool disable_ping_ack) {
130   g_disable_ping_ack = disable_ping_ack;
131 }