• 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 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_CHTTP2_TRANSPORT_H
20 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_CHTTP2_TRANSPORT_H
21 
22 #include <grpc/slice.h>
23 #include <grpc/support/port_platform.h>
24 
25 #include <cstdint>
26 #include <string>
27 
28 #include "absl/types/optional.h"
29 #include "src/core/channelz/channelz.h"
30 #include "src/core/ext/transport/chttp2/transport/flow_control.h"
31 #include "src/core/lib/channel/channel_args.h"
32 #include "src/core/lib/debug/trace.h"
33 #include "src/core/lib/iomgr/buffer_list.h"
34 #include "src/core/lib/iomgr/closure.h"
35 #include "src/core/lib/iomgr/endpoint.h"
36 #include "src/core/lib/iomgr/error.h"
37 #include "src/core/lib/transport/transport.h"
38 #include "src/core/telemetry/call_tracer.h"
39 #include "src/core/util/ref_counted_ptr.h"
40 #include "src/core/util/time.h"
41 
42 /// Creates a CHTTP2 Transport. This takes ownership of a \a resource_user ref
43 /// from the caller; if the caller still needs the resource_user after creating
44 /// a transport, the caller must take another ref.
45 grpc_core::Transport* grpc_create_chttp2_transport(
46     const grpc_core::ChannelArgs& channel_args,
47     grpc_core::OrphanablePtr<grpc_endpoint> ep, bool is_client);
48 
49 grpc_core::RefCountedPtr<grpc_core::channelz::SocketNode>
50 grpc_chttp2_transport_get_socket_node(grpc_core::Transport* transport);
51 
52 /// Takes ownership of \a read_buffer, which (if non-NULL) contains
53 /// leftover bytes previously read from the endpoint (e.g., by handshakers).
54 /// If non-null, \a notify_on_receive_settings will be scheduled when
55 /// HTTP/2 settings are received from the peer.
56 /// If non-null, the endpoint will be removed from
57 /// interested_parties_until_recv_settings before
58 /// notify_on_receive_settings is invoked.
59 void grpc_chttp2_transport_start_reading(
60     grpc_core::Transport* transport, grpc_slice_buffer* read_buffer,
61     grpc_closure* notify_on_receive_settings,
62     grpc_pollset_set* interested_parties_until_recv_settings,
63     grpc_closure* notify_on_close);
64 
65 namespace grpc_core {
66 typedef void (*TestOnlyGlobalHttp2TransportInitCallback)();
67 typedef void (*TestOnlyGlobalHttp2TransportDestructCallback)();
68 
69 void TestOnlySetGlobalHttp2TransportInitCallback(
70     TestOnlyGlobalHttp2TransportInitCallback callback);
71 
72 void TestOnlySetGlobalHttp2TransportDestructCallback(
73     TestOnlyGlobalHttp2TransportDestructCallback callback);
74 
75 // If \a disable is true, the HTTP2 transport will not update the connectivity
76 // state tracker to TRANSIENT_FAILURE when a goaway is received. This prevents
77 // the watchers (eg. client_channel) from noticing the GOAWAY, thereby allowing
78 // us to test the racy behavior when a call is sent down the stack around the
79 // same time that a GOAWAY is received.
80 void TestOnlyGlobalHttp2TransportDisableTransientFailureStateNotification(
81     bool disable);
82 
83 typedef void (*WriteTimestampsCallback)(void*, Timestamps*,
84                                         grpc_error_handle error);
85 typedef void* (*CopyContextFn)(Arena*);
86 
87 void GrpcHttp2SetWriteTimestampsCallback(WriteTimestampsCallback fn);
88 void GrpcHttp2SetCopyContextFn(CopyContextFn fn);
89 
90 WriteTimestampsCallback GrpcHttp2GetWriteTimestampsCallback();
91 CopyContextFn GrpcHttp2GetCopyContextFn();
92 
93 // Interprets the passed arg as a ContextList type and for each entry in the
94 // passed ContextList, it executes the function set using
95 // GrpcHttp2SetWriteTimestampsCallback method with each context in the list
96 // and \a ts. It also deletes/frees up the passed ContextList after this
97 // operation.
98 void ForEachContextListEntryExecute(void* arg, Timestamps* ts,
99                                     grpc_error_handle error);
100 
101 class HttpAnnotation : public CallTracerAnnotationInterface::Annotation {
102  public:
103   enum class Type : uint8_t {
104     kUnknown = 0,
105     // When the first byte enters the HTTP transport.
106     kStart,
107     // When the first byte leaves the HTTP transport.
108     kHeadWritten,
109     // When the last byte leaves the HTTP transport.
110     kEnd,
111   };
112 
113   // A snapshot of write stats to export.
114   struct WriteStats {
115     size_t target_write_size;
116   };
117 
118   HttpAnnotation(Type type, gpr_timespec time);
119 
Add(const chttp2::TransportFlowControl::Stats & stats)120   HttpAnnotation& Add(const chttp2::TransportFlowControl::Stats& stats) {
121     transport_stats_ = stats;
122     return *this;
123   }
124 
Add(const chttp2::StreamFlowControl::Stats & stats)125   HttpAnnotation& Add(const chttp2::StreamFlowControl::Stats& stats) {
126     stream_stats_ = stats;
127     return *this;
128   }
129 
Add(const WriteStats & stats)130   HttpAnnotation& Add(const WriteStats& stats) {
131     write_stats_ = stats;
132     return *this;
133   }
134 
135   std::string ToString() const override;
136 
http_type()137   Type http_type() const { return type_; }
time()138   gpr_timespec time() const { return time_; }
transport_stats()139   absl::optional<chttp2::TransportFlowControl::Stats> transport_stats() const {
140     return transport_stats_;
141   }
stream_stats()142   absl::optional<chttp2::StreamFlowControl::Stats> stream_stats() const {
143     return stream_stats_;
144   }
write_stats()145   absl::optional<WriteStats> write_stats() const { return write_stats_; }
146 
147  private:
148   const Type type_;
149   const gpr_timespec time_;
150   absl::optional<chttp2::TransportFlowControl::Stats> transport_stats_;
151   absl::optional<chttp2::StreamFlowControl::Stats> stream_stats_;
152   absl::optional<WriteStats> write_stats_;
153 };
154 
155 }  // namespace grpc_core
156 
157 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_CHTTP2_TRANSPORT_H
158