• 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 "src/core/ext/transport/chttp2/transport/frame_rst_stream.h"
20 
21 #include <grpc/slice_buffer.h>
22 #include <grpc/support/port_platform.h>
23 #include <stddef.h>
24 
25 #include "absl/log/check.h"
26 #include "absl/log/log.h"
27 #include "absl/random/distributions.h"
28 #include "absl/status/status.h"
29 #include "absl/strings/str_cat.h"
30 #include "absl/strings/str_format.h"
31 #include "src/core/ext/transport/chttp2/transport/call_tracer_wrapper.h"
32 #include "src/core/ext/transport/chttp2/transport/internal.h"
33 #include "src/core/ext/transport/chttp2/transport/legacy_frame.h"
34 #include "src/core/ext/transport/chttp2/transport/ping_callbacks.h"
35 #include "src/core/lib/debug/trace.h"
36 #include "src/core/lib/experiments/experiments.h"
37 #include "src/core/lib/transport/http2_errors.h"
38 #include "src/core/lib/transport/metadata_batch.h"
39 #include "src/core/util/status_helper.h"
40 
grpc_chttp2_rst_stream_create(uint32_t id,uint32_t code,grpc_core::CallTracerInterface * call_tracer)41 grpc_slice grpc_chttp2_rst_stream_create(
42     uint32_t id, uint32_t code, grpc_core::CallTracerInterface* call_tracer) {
43   static const size_t frame_size = 13;
44   grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
45   if (call_tracer != nullptr) {
46     call_tracer->RecordOutgoingBytes({frame_size, 0, 0});
47   }
48   uint8_t* p = GRPC_SLICE_START_PTR(slice);
49 
50   // Frame size.
51   *p++ = 0;
52   *p++ = 0;
53   *p++ = 4;
54   // Frame type.
55   *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
56   // Flags.
57   *p++ = 0;
58   // Stream ID.
59   *p++ = static_cast<uint8_t>(id >> 24);
60   *p++ = static_cast<uint8_t>(id >> 16);
61   *p++ = static_cast<uint8_t>(id >> 8);
62   *p++ = static_cast<uint8_t>(id);
63   // Error code.
64   *p++ = static_cast<uint8_t>(code >> 24);
65   *p++ = static_cast<uint8_t>(code >> 16);
66   *p++ = static_cast<uint8_t>(code >> 8);
67   *p++ = static_cast<uint8_t>(code);
68 
69   return slice;
70 }
71 
grpc_chttp2_add_rst_stream_to_next_write(grpc_chttp2_transport * t,uint32_t id,uint32_t code,grpc_core::CallTracerInterface * call_tracer)72 void grpc_chttp2_add_rst_stream_to_next_write(
73     grpc_chttp2_transport* t, uint32_t id, uint32_t code,
74     grpc_core::CallTracerInterface* call_tracer) {
75   t->num_pending_induced_frames++;
76   grpc_slice_buffer_add(&t->qbuf,
77                         grpc_chttp2_rst_stream_create(id, code, call_tracer));
78 }
79 
grpc_chttp2_rst_stream_parser_begin_frame(grpc_chttp2_rst_stream_parser * parser,uint32_t length,uint8_t flags)80 grpc_error_handle grpc_chttp2_rst_stream_parser_begin_frame(
81     grpc_chttp2_rst_stream_parser* parser, uint32_t length, uint8_t flags) {
82   if (length != 4) {
83     return GRPC_ERROR_CREATE(absl::StrFormat(
84         "invalid rst_stream: length=%d, flags=%02x", length, flags));
85   }
86   parser->byte = 0;
87   return absl::OkStatus();
88 }
89 
grpc_chttp2_rst_stream_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream * s,const grpc_slice & slice,int is_last)90 grpc_error_handle grpc_chttp2_rst_stream_parser_parse(void* parser,
91                                                       grpc_chttp2_transport* t,
92                                                       grpc_chttp2_stream* s,
93                                                       const grpc_slice& slice,
94                                                       int is_last) {
95   const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
96   const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
97   const uint8_t* cur = beg;
98   grpc_chttp2_rst_stream_parser* p =
99       static_cast<grpc_chttp2_rst_stream_parser*>(parser);
100 
101   while (p->byte != 4 && cur != end) {
102     p->reason_bytes[p->byte] = *cur;
103     cur++;
104     p->byte++;
105   }
106   uint64_t framing_bytes = static_cast<uint64_t>(end - cur);
107   s->call_tracer_wrapper.RecordIncomingBytes({framing_bytes, 0, 0});
108 
109   if (p->byte == 4) {
110     CHECK(is_last);
111     uint32_t reason = ((static_cast<uint32_t>(p->reason_bytes[0])) << 24) |
112                       ((static_cast<uint32_t>(p->reason_bytes[1])) << 16) |
113                       ((static_cast<uint32_t>(p->reason_bytes[2])) << 8) |
114                       ((static_cast<uint32_t>(p->reason_bytes[3])));
115     GRPC_TRACE_LOG(http, INFO)
116         << "[chttp2 transport=" << t << " stream=" << s
117         << "] received RST_STREAM(reason=" << reason << ")";
118     grpc_error_handle error;
119     if (reason != GRPC_HTTP2_NO_ERROR || s->trailing_metadata_buffer.empty()) {
120       error = grpc_error_set_int(
121           grpc_error_set_str(
122               GRPC_ERROR_CREATE("RST_STREAM"),
123               grpc_core::StatusStrProperty::kGrpcMessage,
124               absl::StrCat("Received RST_STREAM with error code ", reason)),
125           grpc_core::StatusIntProperty::kHttp2Error,
126           static_cast<intptr_t>(reason));
127     }
128     if (!t->is_client &&
129         absl::Bernoulli(t->bitgen, t->ping_on_rst_stream_percent / 100.0)) {
130       ++t->num_pending_induced_frames;
131       t->ping_callbacks.RequestPing();
132       grpc_chttp2_initiate_write(t, GRPC_CHTTP2_INITIATE_WRITE_KEEPALIVE_PING);
133     }
134     grpc_chttp2_mark_stream_closed(t, s, true, true, error);
135   }
136 
137   return absl::OkStatus();
138 }
139