• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/core/ext/transport/chttp2/transport/internal.h"
23 
24 #include <string.h>
25 
26 #include "absl/strings/str_format.h"
27 
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 
31 static bool g_disable_ping_ack = false;
32 
grpc_chttp2_ping_create(uint8_t ack,uint64_t opaque_8bytes)33 grpc_slice grpc_chttp2_ping_create(uint8_t ack, uint64_t opaque_8bytes) {
34   grpc_slice slice = GRPC_SLICE_MALLOC(9 + 8);
35   uint8_t* p = GRPC_SLICE_START_PTR(slice);
36 
37   *p++ = 0;
38   *p++ = 0;
39   *p++ = 8;
40   *p++ = GRPC_CHTTP2_FRAME_PING;
41   *p++ = ack ? 1 : 0;
42   *p++ = 0;
43   *p++ = 0;
44   *p++ = 0;
45   *p++ = 0;
46   *p++ = static_cast<uint8_t>(opaque_8bytes >> 56);
47   *p++ = static_cast<uint8_t>(opaque_8bytes >> 48);
48   *p++ = static_cast<uint8_t>(opaque_8bytes >> 40);
49   *p++ = static_cast<uint8_t>(opaque_8bytes >> 32);
50   *p++ = static_cast<uint8_t>(opaque_8bytes >> 24);
51   *p++ = static_cast<uint8_t>(opaque_8bytes >> 16);
52   *p++ = static_cast<uint8_t>(opaque_8bytes >> 8);
53   *p++ = static_cast<uint8_t>(opaque_8bytes);
54 
55   return slice;
56 }
57 
grpc_chttp2_ping_parser_begin_frame(grpc_chttp2_ping_parser * parser,uint32_t length,uint8_t flags)58 grpc_error* grpc_chttp2_ping_parser_begin_frame(grpc_chttp2_ping_parser* parser,
59                                                 uint32_t length,
60                                                 uint8_t flags) {
61   if (flags & 0xfe || length != 8) {
62     return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
63         absl::StrFormat("invalid ping: length=%d, flags=%02x", length, flags)
64             .c_str());
65   }
66   parser->byte = 0;
67   parser->is_ack = flags;
68   parser->opaque_8bytes = 0;
69   return GRPC_ERROR_NONE;
70 }
71 
grpc_chttp2_ping_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream *,const grpc_slice & slice,int is_last)72 grpc_error* grpc_chttp2_ping_parser_parse(void* parser,
73                                           grpc_chttp2_transport* t,
74                                           grpc_chttp2_stream* /*s*/,
75                                           const grpc_slice& slice,
76                                           int is_last) {
77   const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
78   const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
79   const uint8_t* cur = beg;
80   grpc_chttp2_ping_parser* p = static_cast<grpc_chttp2_ping_parser*>(parser);
81 
82   while (p->byte != 8 && cur != end) {
83     p->opaque_8bytes |= ((static_cast<uint64_t>(*cur)) << (56 - 8 * p->byte));
84     cur++;
85     p->byte++;
86   }
87 
88   if (p->byte == 8) {
89     GPR_ASSERT(is_last);
90     if (p->is_ack) {
91       grpc_chttp2_ack_ping(t, p->opaque_8bytes);
92     } else {
93       if (!t->is_client) {
94         grpc_millis now = grpc_core::ExecCtx::Get()->Now();
95         grpc_millis next_allowed_ping =
96             t->ping_recv_state.last_ping_recv_time +
97             t->ping_policy.min_recv_ping_interval_without_data;
98 
99         if (t->keepalive_permit_without_calls == 0 &&
100             grpc_chttp2_stream_map_size(&t->stream_map) == 0) {
101           /* According to RFC1122, the interval of TCP Keep-Alive is default to
102              no less than two hours. When there is no outstanding streams, we
103              restrict the number of PINGS equivalent to TCP Keep-Alive. */
104           next_allowed_ping =
105               t->ping_recv_state.last_ping_recv_time + 7200 * GPR_MS_PER_SEC;
106         }
107 
108         if (next_allowed_ping > now) {
109           grpc_chttp2_add_ping_strike(t);
110         }
111 
112         t->ping_recv_state.last_ping_recv_time = now;
113       }
114       if (!g_disable_ping_ack) {
115         if (t->ping_ack_count == t->ping_ack_capacity) {
116           t->ping_ack_capacity = GPR_MAX(t->ping_ack_capacity * 3 / 2, 3);
117           t->ping_acks = static_cast<uint64_t*>(gpr_realloc(
118               t->ping_acks, t->ping_ack_capacity * sizeof(*t->ping_acks)));
119         }
120         t->num_pending_induced_frames++;
121         t->ping_acks[t->ping_ack_count++] = p->opaque_8bytes;
122         grpc_chttp2_initiate_write(t, GRPC_CHTTP2_INITIATE_WRITE_PING_RESPONSE);
123       }
124     }
125   }
126 
127   return GRPC_ERROR_NONE;
128 }
129 
grpc_set_disable_ping_ack(bool disable_ping_ack)130 void grpc_set_disable_ping_ack(bool disable_ping_ack) {
131   g_disable_ping_ack = disable_ping_ack;
132 }
133