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