frame_goaway.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 "absl/base/attributes.h"
26 #include "absl/strings/str_format.h"
27 #include "absl/strings/string_view.h"
28 
29 #include <grpc/slice_buffer.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 
34 
36  p->debug_data = nullptr;
37 }
38 
40  gpr_free(p->debug_data);
41 }
42 
45  if (length < 8) {
47  absl::StrFormat("goaway frame too short (%d bytes)", length));
48  }
49 
50  gpr_free(p->debug_data);
51  p->debug_length = length - 8;
52  p->debug_data = static_cast<char*>(gpr_malloc(p->debug_length));
53  p->debug_pos = 0;
54  p->state = GRPC_CHTTP2_GOAWAY_LSI0;
55  return GRPC_ERROR_NONE;
56 }
57 
60  grpc_chttp2_stream* /*s*/,
61  const grpc_slice& slice,
62  int is_last) {
63  const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
64  const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
65  const uint8_t* cur = beg;
67  static_cast<grpc_chttp2_goaway_parser*>(parser);
68 
69  switch (p->state) {
71  if (cur == end) {
72  p->state = GRPC_CHTTP2_GOAWAY_LSI0;
73  return GRPC_ERROR_NONE;
74  }
75  p->last_stream_id = (static_cast<uint32_t>(*cur)) << 24;
76  ++cur;
79  if (cur == end) {
80  p->state = GRPC_CHTTP2_GOAWAY_LSI1;
81  return GRPC_ERROR_NONE;
82  }
83  p->last_stream_id |= (static_cast<uint32_t>(*cur)) << 16;
84  ++cur;
87  if (cur == end) {
88  p->state = GRPC_CHTTP2_GOAWAY_LSI2;
89  return GRPC_ERROR_NONE;
90  }
91  p->last_stream_id |= (static_cast<uint32_t>(*cur)) << 8;
92  ++cur;
95  if (cur == end) {
96  p->state = GRPC_CHTTP2_GOAWAY_LSI3;
97  return GRPC_ERROR_NONE;
98  }
99  p->last_stream_id |= (static_cast<uint32_t>(*cur));
100  ++cur;
103  if (cur == end) {
104  p->state = GRPC_CHTTP2_GOAWAY_ERR0;
105  return GRPC_ERROR_NONE;
106  }
107  p->error_code = (static_cast<uint32_t>(*cur)) << 24;
108  ++cur;
111  if (cur == end) {
112  p->state = GRPC_CHTTP2_GOAWAY_ERR1;
113  return GRPC_ERROR_NONE;
114  }
115  p->error_code |= (static_cast<uint32_t>(*cur)) << 16;
116  ++cur;
119  if (cur == end) {
120  p->state = GRPC_CHTTP2_GOAWAY_ERR2;
121  return GRPC_ERROR_NONE;
122  }
123  p->error_code |= (static_cast<uint32_t>(*cur)) << 8;
124  ++cur;
127  if (cur == end) {
128  p->state = GRPC_CHTTP2_GOAWAY_ERR3;
129  return GRPC_ERROR_NONE;
130  }
131  p->error_code |= (static_cast<uint32_t>(*cur));
132  ++cur;
135  if (end != cur) {
136  memcpy(p->debug_data + p->debug_pos, cur,
137  static_cast<size_t>(end - cur));
138  }
139  GPR_ASSERT((size_t)(end - cur) < UINT32_MAX - p->debug_pos);
140  p->debug_pos += static_cast<uint32_t>(end - cur);
141  p->state = GRPC_CHTTP2_GOAWAY_DEBUG;
142  if (is_last) {
144  t, p->error_code, p->last_stream_id,
145  absl::string_view(p->debug_data, p->debug_length));
146  gpr_free(p->debug_data);
147  p->debug_data = nullptr;
148  }
149  return GRPC_ERROR_NONE;
150  }
152  return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Should never reach here"));
153 }
154 
155 void grpc_chttp2_goaway_append(uint32_t last_stream_id, uint32_t error_code,
156  const grpc_slice& debug_data,
157  grpc_slice_buffer* slice_buffer) {
158  grpc_slice header = GRPC_SLICE_MALLOC(9 + 4 + 4);
161  GPR_ASSERT(GRPC_SLICE_LENGTH(debug_data) < UINT32_MAX - 4 - 4);
162  frame_length = 4 + 4 + static_cast<uint32_t> GRPC_SLICE_LENGTH(debug_data);
163 
164  /* frame header: length */
165  *p++ = static_cast<uint8_t>(frame_length >> 16);
166  *p++ = static_cast<uint8_t>(frame_length >> 8);
167  *p++ = static_cast<uint8_t>(frame_length);
168  /* frame header: type */
170  /* frame header: flags */
171  *p++ = 0;
172  /* frame header: stream id */
173  *p++ = 0;
174  *p++ = 0;
175  *p++ = 0;
176  *p++ = 0;
177  /* payload: last stream id */
178  *p++ = static_cast<uint8_t>(last_stream_id >> 24);
179  *p++ = static_cast<uint8_t>(last_stream_id >> 16);
180  *p++ = static_cast<uint8_t>(last_stream_id >> 8);
181  *p++ = static_cast<uint8_t>(last_stream_id);
182  /* payload: error code */
183  *p++ = static_cast<uint8_t>(error_code >> 24);
184  *p++ = static_cast<uint8_t>(error_code >> 16);
185  *p++ = static_cast<uint8_t>(error_code >> 8);
186  *p++ = static_cast<uint8_t>(error_code);
188  grpc_slice_buffer_add(slice_buffer, header);
189  grpc_slice_buffer_add(slice_buffer, debug_data);
190 }
GRPC_CHTTP2_GOAWAY_LSI3
@ GRPC_CHTTP2_GOAWAY_LSI3
Definition: frame_goaway.h:35
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
grpc_chttp2_goaway_parser_destroy
void grpc_chttp2_goaway_parser_destroy(grpc_chttp2_goaway_parser *p)
Definition: frame_goaway.cc:39
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
GRPC_CHTTP2_GOAWAY_ERR2
@ GRPC_CHTTP2_GOAWAY_ERR2
Definition: frame_goaway.h:38
internal.h
GRPC_CHTTP2_GOAWAY_LSI2
@ GRPC_CHTTP2_GOAWAY_LSI2
Definition: frame_goaway.h:34
string.h
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
grpc_chttp2_goaway_parser
Definition: frame_goaway.h:43
grpc_chttp2_add_incoming_goaway
void grpc_chttp2_add_incoming_goaway(grpc_chttp2_transport *t, uint32_t goaway_error, uint32_t last_stream_id, absl::string_view goaway_text)
Definition: chttp2_transport.cc:1099
UINT32_MAX
#define UINT32_MAX
Definition: stdint-msvc2008.h:142
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
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
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
frame_length
static size_t frame_length(size_t payload_length)
Definition: frame_handler_test.cc:42
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
grpc_chttp2_goaway_append
void grpc_chttp2_goaway_append(uint32_t last_stream_id, uint32_t error_code, const grpc_slice &debug_data, grpc_slice_buffer *slice_buffer)
Definition: frame_goaway.cc:155
grpc_chttp2_goaway_parser_begin_frame
grpc_error_handle grpc_chttp2_goaway_parser_begin_frame(grpc_chttp2_goaway_parser *p, uint32_t length, uint8_t)
Definition: frame_goaway.cc:43
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
GRPC_CHTTP2_GOAWAY_ERR3
@ GRPC_CHTTP2_GOAWAY_ERR3
Definition: frame_goaway.h:39
grpc_chttp2_goaway_parser_parse
grpc_error_handle grpc_chttp2_goaway_parser_parse(void *parser, grpc_chttp2_transport *t, grpc_chttp2_stream *, const grpc_slice &slice, int is_last)
Definition: frame_goaway.cc:58
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
header
struct absl::base_internal::@2940::AllocList::Header header
frame_goaway.h
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
slice_buffer.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: impl/codegen/port_platform.h:652
GRPC_SLICE_END_PTR
#define GRPC_SLICE_END_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:110
GRPC_CHTTP2_FRAME_GOAWAY
#define GRPC_CHTTP2_FRAME_GOAWAY
Definition: frame.h:34
GRPC_CHTTP2_GOAWAY_LSI0
@ GRPC_CHTTP2_GOAWAY_LSI0
Definition: frame_goaway.h:32
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
GRPC_CHTTP2_GOAWAY_DEBUG
@ GRPC_CHTTP2_GOAWAY_DEBUG
Definition: frame_goaway.h:40
GRPC_CHTTP2_GOAWAY_ERR1
@ GRPC_CHTTP2_GOAWAY_ERR1
Definition: frame_goaway.h:37
memory_diff.cur
def cur
Definition: memory_diff.py:83
GRPC_CHTTP2_GOAWAY_LSI1
@ GRPC_CHTTP2_GOAWAY_LSI1
Definition: frame_goaway.h:33
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
GRPC_CHTTP2_GOAWAY_ERR0
@ GRPC_CHTTP2_GOAWAY_ERR0
Definition: frame_goaway.h:36
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
grpc_chttp2_goaway_parser_init
void grpc_chttp2_goaway_parser_init(grpc_chttp2_goaway_parser *p)
Definition: frame_goaway.cc:35
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
grpc_error
Definition: error_internal.h:42
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
ABSL_FALLTHROUGH_INTENDED
#define ABSL_FALLTHROUGH_INTENDED
Definition: abseil-cpp/absl/base/attributes.h:641
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:25