frame_ping.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <string.h>
24 
25 #include <algorithm>
26 
27 #include "absl/strings/str_format.h"
28 
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 
36 
37 static bool g_disable_ping_ack = false;
38 
42 
43  *p++ = 0;
44  *p++ = 0;
45  *p++ = 8;
47  *p++ = ack ? 1 : 0;
48  *p++ = 0;
49  *p++ = 0;
50  *p++ = 0;
51  *p++ = 0;
52  *p++ = static_cast<uint8_t>(opaque_8bytes >> 56);
53  *p++ = static_cast<uint8_t>(opaque_8bytes >> 48);
54  *p++ = static_cast<uint8_t>(opaque_8bytes >> 40);
55  *p++ = static_cast<uint8_t>(opaque_8bytes >> 32);
56  *p++ = static_cast<uint8_t>(opaque_8bytes >> 24);
57  *p++ = static_cast<uint8_t>(opaque_8bytes >> 16);
58  *p++ = static_cast<uint8_t>(opaque_8bytes >> 8);
59  *p++ = static_cast<uint8_t>(opaque_8bytes);
60 
61  return slice;
62 }
63 
66  if (flags & 0xfe || length != 8) {
68  absl::StrFormat("invalid ping: length=%d, flags=%02x", length, flags));
69  }
70  parser->byte = 0;
71  parser->is_ack = flags;
72  parser->opaque_8bytes = 0;
73  return GRPC_ERROR_NONE;
74 }
75 
78  grpc_chttp2_stream* /*s*/,
79  const grpc_slice& slice,
80  int is_last) {
81  const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
82  const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
83  const uint8_t* cur = beg;
85 
86  while (p->byte != 8 && cur != end) {
87  p->opaque_8bytes |= ((static_cast<uint64_t>(*cur)) << (56 - 8 * p->byte));
88  cur++;
89  p->byte++;
90  }
91 
92  if (p->byte == 8) {
93  GPR_ASSERT(is_last);
94  if (p->is_ack) {
95  grpc_chttp2_ack_ping(t, p->opaque_8bytes);
96  } else {
97  if (!t->is_client) {
99  grpc_core::Timestamp next_allowed_ping =
100  t->ping_recv_state.last_ping_recv_time +
101  t->ping_policy.min_recv_ping_interval_without_data;
102 
103  if (t->keepalive_permit_without_calls == 0 &&
104  grpc_chttp2_stream_map_size(&t->stream_map) == 0) {
105  /* According to RFC1122, the interval of TCP Keep-Alive is default to
106  no less than two hours. When there is no outstanding streams, we
107  restrict the number of PINGS equivalent to TCP Keep-Alive. */
108  next_allowed_ping = t->ping_recv_state.last_ping_recv_time +
110  }
111 
112  if (next_allowed_ping > now) {
114  }
115 
116  t->ping_recv_state.last_ping_recv_time = now;
117  }
118  if (!g_disable_ping_ack) {
119  if (t->ping_ack_count == t->ping_ack_capacity) {
120  t->ping_ack_capacity =
121  std::max(t->ping_ack_capacity * 3 / 2, size_t(3));
122  t->ping_acks = static_cast<uint64_t*>(gpr_realloc(
123  t->ping_acks, t->ping_ack_capacity * sizeof(*t->ping_acks)));
124  }
125  t->num_pending_induced_frames++;
126  t->ping_acks[t->ping_ack_count++] = p->opaque_8bytes;
128  }
129  }
130  }
131 
132  return GRPC_ERROR_NONE;
133 }
134 
135 void grpc_set_disable_ping_ack(bool disable_ping_ack) {
136  g_disable_ping_ack = disable_ping_ack;
137 }
grpc_core::Duration::Hours
static constexpr Duration Hours(int64_t hours)
Definition: src/core/lib/gprpp/time.h:143
now
static double now(void)
Definition: test/core/fling/client.cc:130
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
internal.h
grpc_chttp2_ping_parser_begin_frame
grpc_error_handle grpc_chttp2_ping_parser_begin_frame(grpc_chttp2_ping_parser *parser, uint32_t length, uint8_t flags)
Definition: frame_ping.cc:64
string.h
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_chttp2_add_ping_strike
void grpc_chttp2_add_ping_strike(grpc_chttp2_transport *t)
Definition: chttp2_transport.cc:1845
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
xds_manager.p
p
Definition: xds_manager.py:60
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_chttp2_stream
Definition: src/core/ext/transport/chttp2/transport/internal.h:456
grpc_chttp2_ack_ping
void grpc_chttp2_ack_ping(grpc_chttp2_transport *t, uint64_t id)
Definition: chttp2_transport.cc:1704
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
g_disable_ping_ack
static bool g_disable_ping_ack
Definition: frame_ping.cc:37
asyncio_get_stats.parser
parser
Definition: asyncio_get_stats.py:34
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
GRPC_CHTTP2_FRAME_PING
#define GRPC_CHTTP2_FRAME_PING
Definition: frame.h:33
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
time.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
frame_ping.h
grpc_chttp2_ping_create
grpc_slice grpc_chttp2_ping_create(uint8_t ack, uint64_t opaque_8bytes)
Definition: frame_ping.cc:39
GRPC_SLICE_END_PTR
#define GRPC_SLICE_END_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:110
stream_map.h
grpc_chttp2_stream_map_size
size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map *map)
Definition: stream_map.cc:150
grpc_chttp2_ping_parser
Definition: frame_ping.h:31
memory_diff.cur
def cur
Definition: memory_diff.py:83
GRPC_CHTTP2_INITIATE_WRITE_PING_RESPONSE
@ GRPC_CHTTP2_INITIATE_WRITE_PING_RESPONSE
Definition: src/core/ext/transport/chttp2/transport/internal.h:128
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
grpc_set_disable_ping_ack
void grpc_set_disable_ping_ack(bool disable_ping_ack)
Definition: frame_ping.cc:135
grpc_chttp2_initiate_write
void grpc_chttp2_initiate_write(grpc_chttp2_transport *t, grpc_chttp2_initiate_write_reason reason)
Definition: chttp2_transport.cc:891
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
alloc.h
grpc_chttp2_transport
Definition: src/core/ext/transport/chttp2/transport/internal.h:238
exec_ctx.h
flags
uint32_t flags
Definition: retry_filter.cc:632
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_error
Definition: error_internal.h:42
grpc_chttp2_ping_parser_parse
grpc_error_handle grpc_chttp2_ping_parser_parse(void *parser, grpc_chttp2_transport *t, grpc_chttp2_stream *, const grpc_slice &slice, int is_last)
Definition: frame_ping.cc:76
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22