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