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