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_rst_stream.h"
22 
23 #include <stddef.h>
24 
25 #include <initializer_list>
26 
27 #include "absl/status/status.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30 
31 #include <grpc/slice_buffer.h>
32 #include <grpc/support/log.h>
33 
34 #include "src/core/ext/transport/chttp2/transport/frame.h"
35 #include "src/core/ext/transport/chttp2/transport/http_trace.h"
36 #include "src/core/ext/transport/chttp2/transport/internal.h"
37 #include "src/core/lib/debug/trace.h"
38 #include "src/core/lib/gprpp/status_helper.h"
39 #include "src/core/lib/transport/http2_errors.h"
40 #include "src/core/lib/transport/metadata_batch.h"
41 
grpc_chttp2_rst_stream_create(uint32_t id,uint32_t code,grpc_transport_one_way_stats * stats)42 grpc_slice grpc_chttp2_rst_stream_create(uint32_t id, uint32_t code,
43                                          grpc_transport_one_way_stats* stats) {
44   static const size_t frame_size = 13;
45   grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
46   if (stats != nullptr) stats->framing_bytes += frame_size;
47   uint8_t* p = GRPC_SLICE_START_PTR(slice);
48 
49   // Frame size.
50   *p++ = 0;
51   *p++ = 0;
52   *p++ = 4;
53   // Frame type.
54   *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
55   // Flags.
56   *p++ = 0;
57   // Stream ID.
58   *p++ = static_cast<uint8_t>(id >> 24);
59   *p++ = static_cast<uint8_t>(id >> 16);
60   *p++ = static_cast<uint8_t>(id >> 8);
61   *p++ = static_cast<uint8_t>(id);
62   // Error code.
63   *p++ = static_cast<uint8_t>(code >> 24);
64   *p++ = static_cast<uint8_t>(code >> 16);
65   *p++ = static_cast<uint8_t>(code >> 8);
66   *p++ = static_cast<uint8_t>(code);
67 
68   return slice;
69 }
70 
grpc_chttp2_add_rst_stream_to_next_write(grpc_chttp2_transport * t,uint32_t id,uint32_t code,grpc_transport_one_way_stats * stats)71 void grpc_chttp2_add_rst_stream_to_next_write(
72     grpc_chttp2_transport* t, uint32_t id, uint32_t code,
73     grpc_transport_one_way_stats* stats) {
74   t->num_pending_induced_frames++;
75   grpc_slice_buffer_add(&t->qbuf,
76                         grpc_chttp2_rst_stream_create(id, code, stats));
77 }
78 
grpc_chttp2_rst_stream_parser_begin_frame(grpc_chttp2_rst_stream_parser * parser,uint32_t length,uint8_t flags)79 grpc_error_handle grpc_chttp2_rst_stream_parser_begin_frame(
80     grpc_chttp2_rst_stream_parser* parser, uint32_t length, uint8_t flags) {
81   if (length != 4) {
82     return GRPC_ERROR_CREATE(absl::StrFormat(
83         "invalid rst_stream: length=%d, flags=%02x", length, flags));
84   }
85   parser->byte = 0;
86   return absl::OkStatus();
87 }
88 
grpc_chttp2_rst_stream_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream * s,const grpc_slice & slice,int is_last)89 grpc_error_handle grpc_chttp2_rst_stream_parser_parse(void* parser,
90                                                       grpc_chttp2_transport* t,
91                                                       grpc_chttp2_stream* s,
92                                                       const grpc_slice& slice,
93                                                       int is_last) {
94   const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
95   const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
96   const uint8_t* cur = beg;
97   grpc_chttp2_rst_stream_parser* p =
98       static_cast<grpc_chttp2_rst_stream_parser*>(parser);
99 
100   while (p->byte != 4 && cur != end) {
101     p->reason_bytes[p->byte] = *cur;
102     cur++;
103     p->byte++;
104   }
105   s->stats.incoming.framing_bytes += static_cast<uint64_t>(end - cur);
106 
107   if (p->byte == 4) {
108     GPR_ASSERT(is_last);
109     uint32_t reason = ((static_cast<uint32_t>(p->reason_bytes[0])) << 24) |
110                       ((static_cast<uint32_t>(p->reason_bytes[1])) << 16) |
111                       ((static_cast<uint32_t>(p->reason_bytes[2])) << 8) |
112                       ((static_cast<uint32_t>(p->reason_bytes[3])));
113     if (GRPC_TRACE_FLAG_ENABLED(grpc_http_trace)) {
114       gpr_log(GPR_INFO,
115               "[chttp2 transport=%p stream=%p] received RST_STREAM(reason=%d)",
116               t, s, reason);
117     }
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     grpc_chttp2_mark_stream_closed(t, s, true, true, error);
129   }
130 
131   return absl::OkStatus();
132 }
133