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_window_update.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/support/log.h>
32
33 #include "src/core/ext/transport/chttp2/transport/flow_control.h"
34 #include "src/core/ext/transport/chttp2/transport/internal.h"
35
grpc_chttp2_window_update_create(uint32_t id,uint32_t window_delta,grpc_transport_one_way_stats * stats)36 grpc_slice grpc_chttp2_window_update_create(
37 uint32_t id, uint32_t window_delta, grpc_transport_one_way_stats* stats) {
38 static const size_t frame_size = 13;
39 grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
40 stats->header_bytes += frame_size;
41 uint8_t* p = GRPC_SLICE_START_PTR(slice);
42
43 GPR_ASSERT(window_delta);
44
45 *p++ = 0;
46 *p++ = 0;
47 *p++ = 4;
48 *p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE;
49 *p++ = 0;
50 *p++ = static_cast<uint8_t>(id >> 24);
51 *p++ = static_cast<uint8_t>(id >> 16);
52 *p++ = static_cast<uint8_t>(id >> 8);
53 *p++ = static_cast<uint8_t>(id);
54 *p++ = static_cast<uint8_t>(window_delta >> 24);
55 *p++ = static_cast<uint8_t>(window_delta >> 16);
56 *p++ = static_cast<uint8_t>(window_delta >> 8);
57 *p++ = static_cast<uint8_t>(window_delta);
58
59 return slice;
60 }
61
grpc_chttp2_window_update_parser_begin_frame(grpc_chttp2_window_update_parser * parser,uint32_t length,uint8_t flags)62 grpc_error_handle grpc_chttp2_window_update_parser_begin_frame(
63 grpc_chttp2_window_update_parser* parser, uint32_t length, uint8_t flags) {
64 if (flags || length != 4) {
65 return GRPC_ERROR_CREATE(absl::StrFormat(
66 "invalid window update: length=%d, flags=%02x", length, flags));
67 }
68 parser->byte = 0;
69 parser->amount = 0;
70 return absl::OkStatus();
71 }
72
grpc_chttp2_window_update_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream * s,const grpc_slice & slice,int is_last)73 grpc_error_handle grpc_chttp2_window_update_parser_parse(
74 void* parser, grpc_chttp2_transport* t, grpc_chttp2_stream* s,
75 const grpc_slice& slice, int is_last) {
76 const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
77 const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
78 const uint8_t* cur = beg;
79 grpc_chttp2_window_update_parser* p =
80 static_cast<grpc_chttp2_window_update_parser*>(parser);
81
82 while (p->byte != 4 && cur != end) {
83 p->amount |= (static_cast<uint32_t>(*cur)) << (8 * (3 - p->byte));
84 cur++;
85 p->byte++;
86 }
87
88 if (s != nullptr) {
89 s->stats.incoming.framing_bytes += static_cast<uint32_t>(end - cur);
90 }
91
92 if (p->byte == 4) {
93 // top bit is reserved and must be ignored.
94 uint32_t received_update = p->amount & 0x7fffffffu;
95 if (received_update == 0) {
96 return GRPC_ERROR_CREATE(
97 absl::StrCat("invalid window update bytes: ", p->amount));
98 }
99 GPR_ASSERT(is_last);
100
101 if (t->incoming_stream_id != 0) {
102 if (s != nullptr) {
103 grpc_core::chttp2::StreamFlowControl::OutgoingUpdateContext(
104 &s->flow_control)
105 .RecvUpdate(received_update);
106 if (grpc_chttp2_list_remove_stalled_by_stream(t, s)) {
107 grpc_chttp2_mark_stream_writable(t, s);
108 grpc_chttp2_initiate_write(
109 t, GRPC_CHTTP2_INITIATE_WRITE_FLOW_CONTROL_UNSTALLED_BY_UPDATE);
110 }
111 }
112 } else {
113 grpc_core::chttp2::TransportFlowControl::OutgoingUpdateContext upd(
114 &t->flow_control);
115 upd.RecvUpdate(received_update);
116 if (upd.Finish() == grpc_core::chttp2::StallEdge::kUnstalled) {
117 grpc_chttp2_initiate_write(
118 t, GRPC_CHTTP2_INITIATE_WRITE_TRANSPORT_FLOW_CONTROL_UNSTALLED);
119 }
120 }
121 }
122
123 return absl::OkStatus();
124 }
125