• 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_window_update.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <stddef.h>
23 
24 #include "absl/log/check.h"
25 #include "absl/status/status.h"
26 #include "absl/strings/str_cat.h"
27 #include "absl/strings/str_format.h"
28 #include "src/core/ext/transport/chttp2/transport/call_tracer_wrapper.h"
29 #include "src/core/ext/transport/chttp2/transport/flow_control.h"
30 #include "src/core/ext/transport/chttp2/transport/internal.h"
31 #include "src/core/ext/transport/chttp2/transport/stream_lists.h"
32 #include "src/core/telemetry/stats.h"
33 #include "src/core/util/time.h"
34 
grpc_chttp2_window_update_create(uint32_t id,uint32_t window_delta,grpc_core::CallTracerInterface * call_tracer)35 grpc_slice grpc_chttp2_window_update_create(
36     uint32_t id, uint32_t window_delta,
37     grpc_core::CallTracerInterface* call_tracer) {
38   static const size_t frame_size = 13;
39   grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
40   if (call_tracer != nullptr) {
41     call_tracer->RecordOutgoingBytes({frame_size, 0, 0});
42   }
43   uint8_t* p = GRPC_SLICE_START_PTR(slice);
44 
45   CHECK(window_delta);
46 
47   *p++ = 0;
48   *p++ = 0;
49   *p++ = 4;
50   *p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE;
51   *p++ = 0;
52   *p++ = static_cast<uint8_t>(id >> 24);
53   *p++ = static_cast<uint8_t>(id >> 16);
54   *p++ = static_cast<uint8_t>(id >> 8);
55   *p++ = static_cast<uint8_t>(id);
56   *p++ = static_cast<uint8_t>(window_delta >> 24);
57   *p++ = static_cast<uint8_t>(window_delta >> 16);
58   *p++ = static_cast<uint8_t>(window_delta >> 8);
59   *p++ = static_cast<uint8_t>(window_delta);
60 
61   return slice;
62 }
63 
grpc_chttp2_window_update_parser_begin_frame(grpc_chttp2_window_update_parser * parser,uint32_t length,uint8_t flags)64 grpc_error_handle grpc_chttp2_window_update_parser_begin_frame(
65     grpc_chttp2_window_update_parser* parser, uint32_t length, uint8_t flags) {
66   if (flags || length != 4) {
67     return GRPC_ERROR_CREATE(absl::StrFormat(
68         "invalid window update: length=%d, flags=%02x", length, flags));
69   }
70   parser->byte = 0;
71   parser->amount = 0;
72   return absl::OkStatus();
73 }
74 
grpc_chttp2_window_update_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream * s,const grpc_slice & slice,int is_last)75 grpc_error_handle grpc_chttp2_window_update_parser_parse(
76     void* parser, grpc_chttp2_transport* t, grpc_chttp2_stream* s,
77     const grpc_slice& slice, int is_last) {
78   const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
79   const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
80   const uint8_t* cur = beg;
81   grpc_chttp2_window_update_parser* p =
82       static_cast<grpc_chttp2_window_update_parser*>(parser);
83 
84   while (p->byte != 4 && cur != end) {
85     p->amount |= (static_cast<uint32_t>(*cur)) << (8 * (3 - p->byte));
86     cur++;
87     p->byte++;
88   }
89 
90   if (s != nullptr) {
91     uint64_t framing_bytes = static_cast<uint32_t>(end - cur);
92     s->call_tracer_wrapper.RecordIncomingBytes({framing_bytes, 0, 0});
93   }
94 
95   if (p->byte == 4) {
96     // top bit is reserved and must be ignored.
97     uint32_t received_update = p->amount & 0x7fffffffu;
98     if (received_update == 0) {
99       return GRPC_ERROR_CREATE(
100           absl::StrCat("invalid window update bytes: ", p->amount));
101     }
102     CHECK(is_last);
103 
104     if (t->incoming_stream_id != 0) {
105       if (s != nullptr) {
106         grpc_core::Timestamp now = grpc_core::Timestamp::Now();
107         if (s->last_window_update_time != grpc_core::Timestamp::InfPast()) {
108           grpc_core::global_stats().IncrementHttp2StreamWindowUpdatePeriod(
109               (now - s->last_window_update_time).millis());
110         }
111         s->last_window_update_time = now;
112         grpc_core::chttp2::StreamFlowControl::OutgoingUpdateContext(
113             &s->flow_control)
114             .RecvUpdate(received_update);
115         grpc_core::global_stats().IncrementHttp2StreamRemoteWindowUpdate(
116             received_update);
117         if (grpc_chttp2_list_remove_stalled_by_stream(t, s)) {
118           grpc_chttp2_mark_stream_writable(t, s);
119           grpc_chttp2_initiate_write(
120               t, GRPC_CHTTP2_INITIATE_WRITE_FLOW_CONTROL_UNSTALLED_BY_UPDATE);
121         }
122       }
123     } else {
124       grpc_core::chttp2::TransportFlowControl::OutgoingUpdateContext upd(
125           &t->flow_control);
126       grpc_core::Timestamp now = grpc_core::Timestamp::Now();
127       if (t->last_window_update_time != grpc_core::Timestamp::InfPast()) {
128         grpc_core::global_stats().IncrementHttp2TransportWindowUpdatePeriod(
129             (now - t->last_window_update_time).millis());
130       }
131       t->last_window_update_time = now;
132       grpc_core::global_stats().IncrementHttp2TransportRemoteWindowUpdate(
133           received_update);
134       upd.RecvUpdate(received_update);
135       if (upd.Finish() == grpc_core::chttp2::StallEdge::kUnstalled) {
136         grpc_chttp2_initiate_write(
137             t, GRPC_CHTTP2_INITIATE_WRITE_TRANSPORT_FLOW_CONTROL_UNSTALLED);
138       }
139     }
140   }
141 
142   return absl::OkStatus();
143 }
144