Debian Codegraph @

crt/aws-c-http/include/aws/http/private/h2_connection.h

raw symbols SCIP
1 #ifndef AWS_HTTP_H2_CONNECTION_H
2 #define AWS_HTTP_H2_CONNECTION_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/atomics.h>
10 #include <aws/common/fifo_cache.h>
11 #include <aws/common/hash_table.h>
12 #include <aws/common/mutex.h>
13
14 #include <aws/http/private/connection_impl.h>
15 #include <aws/http/private/h2_frames.h>
16 #include <aws/http/statistics.h>
17
18 struct aws_h2_decoder;
19 struct aws_h2_stream;
20
21 struct aws_h2_connection {
22 struct aws_http_connection base;
23
24 aws_http2_on_goaway_received_fn *on_goaway_received;
25 aws_http2_on_remote_settings_change_fn *on_remote_settings_change;
26
27 struct aws_channel_task cross_thread_work_task;
28 struct aws_channel_task outgoing_frames_task;
29
30 bool conn_manual_window_management;
31 /* The threshold to send out a window update frame.
32 * When the window_size_self is less than the threshold, client will starts the sending of WINDOW_UPDATE frame
33 * to keep flow continues.
34 */
35 uint32_t window_size_threshold_to_send_update;
36 /* The threshold to send out a window update frame for all the streams on the connection. Stream window takes int32
37 * since it allows negative.
38 */
39 int32_t stream_window_size_threshold_to_send_update;
40
41 /* Only the event-loop thread may touch this data */
42 struct {
43 struct aws_h2_decoder *decoder;
44 struct aws_h2_frame_encoder encoder;
45
46 /* True when reading/writing has stopped, whether due to errors or normal channel shutdown. */
47 bool is_reading_stopped;
48 bool is_writing_stopped;
49
50 bool is_outgoing_frames_task_active;
51
52 /* Settings received from peer, which restricts the message to send */
53 uint32_t settings_peer[AWS_HTTP2_SETTINGS_END_RANGE];
54 /* Local settings to send/sent to peer, which affects the decoding */
55 uint32_t settings_self[AWS_HTTP2_SETTINGS_END_RANGE];
56
57 /* List using aws_h2_pending_settings.node
58 * Contains settings waiting to be ACKed by peer and applied */
59 struct aws_linked_list pending_settings_queue;
60
61 /* List using aws_h2_pending_ping.node
62 * Pings waiting to be ACKed by peer */
63 struct aws_linked_list pending_ping_queue;
64
65 /* Most recent stream-id that was initiated by peer */
66 uint32_t latest_peer_initiated_stream_id;
67
68 /* Maps stream-id to aws_h2_stream*.
69 * Contains all streams in the open, reserved, and half-closed states (terms from RFC-7540 5.1).
70 * Once a stream enters closed state, it is removed from this map. */
71 struct aws_hash_table active_streams_map;
72
73 /* List using aws_h2_stream.node.
74 * Contains all streams with DATA frames to send.
75 * Any stream in this list is also in the active_streams_map. */
76 struct aws_linked_list outgoing_streams_list;
77
78 /* List using aws_h2_stream.node.
79 * Contains all streams with DATA frames to send, and cannot send now due to flow control.
80 * Waiting for WINDOW_UPDATE to set them free */
81 struct aws_linked_list stalled_window_streams_list;
82
83 /* List using aws_h2_stream.node.
84 * Contains all streams that are open, but are only sending data when notified, rather than polling
85 * for it (e.g. event streams)
86 * Streams are moved to the outgoing_streams_list until they send pending data, then are moved back
87 * to this list to sleep until more data comes in
88 */
89 struct aws_linked_list waiting_streams_list;
90
91 /* List using aws_h2_frame.node.
92 * Queues all frames (except DATA frames) for connection to send.
93 * When queue is empty, then we send DATA frames from the outgoing_streams_list */
94 struct aws_linked_list outgoing_frames_queue;
95
96 /* FIFO cache for closed stream, key: stream-id, value: aws_h2_stream_closed_when.
97 * Contains data about streams that were recently closed.
98 * The oldest entry will be removed if the cache is full */
99 struct aws_cache *closed_streams;
100
101 /* Flow-control of connection from peer. Indicating the buffer capacity of our peer.
102 * Reduce the space after sending a flow-controlled frame. Increment after receiving WINDOW_UPDATE for
103 * connection */
104 uint32_t window_size_peer;
105
106 /* Flow-control of connection for this side.
107 * Reduce the space after receiving a flow-controlled frame. Increment after sending WINDOW_UPDATE for
108 * connection */
109 uint32_t window_size_self;
110 /* The size to increment the window_size_self pending to be sent.
111 * Allow the pending_window_update_size to exceed the max window size.
112 * The client will send the WINDOW_UPDATE frame to the server only valid.
113 * If the pending_window_update_size is too large, we will leave the excess to send it out later.
114 */
115 uint64_t pending_window_update_size_self;
116
117 /* Highest self-initiated stream-id that peer might have processed.
118 * Defaults to max stream-id, may be lowered when GOAWAY frame received. */
119 uint32_t goaway_received_last_stream_id;
120
121 /* Last-stream-id sent in most recent GOAWAY frame. Defaults to max stream-id. */
122 uint32_t goaway_sent_last_stream_id;
123
124 /* Frame we are encoding now. NULL if we are not encoding anything. */
125 struct aws_h2_frame *current_outgoing_frame;
126
127 /* Pointer to initial pending settings. If ACKed by peer, it will be NULL. */
128 struct aws_h2_pending_settings *init_pending_settings;
129
130 /* Cached channel shutdown values.
131 * If possible, we delay shutdown-in-the-write-dir until GOAWAY is written. */
132 int channel_shutdown_error_code;
133 bool channel_shutdown_immediately;
134 bool channel_shutdown_waiting_for_goaway_to_be_written;
135
136 /* TODO: Consider adding stream monitor */
137 struct aws_crt_statistics_http2_channel stats;
138
139 /* Timestamp when connection has data to send, which is when there is an active stream with body to send */
140 uint64_t outgoing_timestamp_ns;
141 /* Timestamp when connection has data to receive, which is when there is an active stream */
142 uint64_t incoming_timestamp_ns;
143
144 } thread_data;
145
146 /* Any thread may touch this data, but the lock must be held (unless it's an atomic) */
147 struct {
148 struct aws_mutex lock;
149
150 /* New `aws_h2_stream *` that haven't moved to `thread_data` yet */
151 struct aws_linked_list pending_stream_list;
152
153 /* New `aws_h2_frames *`, connection control frames created by user that haven't moved to `thread_data` yet */
154 struct aws_linked_list pending_frame_list;
155
156 /* New `aws_h2_pending_settings *` created by user that haven't moved to `thread_data` yet */
157 struct aws_linked_list pending_settings_list;
158
159 /* New `aws_h2_pending_ping *` created by user that haven't moved to `thread_data` yet */
160 struct aws_linked_list pending_ping_list;
161
162 /* New `aws_h2_pending_goaway *` created by user that haven't sent yet */
163 struct aws_linked_list pending_goaway_list;
164
165 bool is_cross_thread_work_task_scheduled;
166
167 /* Value for `thread_data.pending_window_update_size_self` that we haven't applied yet */
168 uint64_t pending_window_update_size_self;
169
170 /* For checking status from outside the event-loop thread. */
171 bool is_open;
172
173 /* If non-zero, reason to immediately reject new streams. (ex: closing) */
174 int new_stream_error_code;
175
176 /* Last-stream-id sent in most recent GOAWAY frame. Defaults to AWS_H2_STREAM_ID_MAX + 1 indicates no GOAWAY has
177 * been sent so far.*/
178 uint32_t goaway_sent_last_stream_id;
179 /* aws_http2_error_code sent in most recent GOAWAY frame. Defaults to 0, check goaway_sent_last_stream_id for
180 * any GOAWAY has sent or not */
181 uint32_t goaway_sent_http2_error_code;
182
183 /* Last-stream-id received in most recent GOAWAY frame. Defaults to AWS_H2_STREAM_ID_MAX + 1 indicates no GOAWAY
184 * has been received so far.*/
185 uint32_t goaway_received_last_stream_id;
186 /* aws_http2_error_code received in most recent GOAWAY frame. Defaults to 0, check
187 * goaway_received_last_stream_id for any GOAWAY has received or not */
188 uint32_t goaway_received_http2_error_code;
189
190 /* For checking settings received from peer from outside the event-loop thread. */
191 uint32_t settings_peer[AWS_HTTP2_SETTINGS_END_RANGE];
192 /* For checking local settings to send/sent to peer from outside the event-loop thread. */
193 uint32_t settings_self[AWS_HTTP2_SETTINGS_END_RANGE];
194 } synced_data;
195 };
196
197 struct aws_h2_pending_settings {
198 struct aws_http2_setting *settings_array;
199 size_t num_settings;
200 struct aws_linked_list_node node;
201 /* user callback */
202 void *user_data;
203 aws_http2_on_change_settings_complete_fn *on_completed;
204 };
205
206 struct aws_h2_pending_ping {
207 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE];
208 /* For calculating round-trip time */
209 uint64_t started_time;
210 struct aws_linked_list_node node;
211 /* user callback */
212 void *user_data;
213 aws_http2_on_ping_complete_fn *on_completed;
214 };
215
216 struct aws_h2_pending_goaway {
217 bool allow_more_streams;
218 uint32_t http2_error;
219 struct aws_byte_cursor debug_data;
220 struct aws_linked_list_node node;
221 };
222
223 /**
224 * The action which caused the stream to close.
225 */
226 enum aws_h2_stream_closed_when {
227 AWS_H2_STREAM_CLOSED_UNKNOWN,
228 AWS_H2_STREAM_CLOSED_WHEN_BOTH_SIDES_END_STREAM,
229 AWS_H2_STREAM_CLOSED_WHEN_RST_STREAM_RECEIVED,
230 AWS_H2_STREAM_CLOSED_WHEN_RST_STREAM_SENT,
231 };
232
233 enum aws_h2_data_encode_status {
234 AWS_H2_DATA_ENCODE_COMPLETE,
235 AWS_H2_DATA_ENCODE_ONGOING,
236 AWS_H2_DATA_ENCODE_ONGOING_BODY_STREAM_STALLED, /* stalled reading from body stream */
237 AWS_H2_DATA_ENCODE_ONGOING_WAITING_FOR_WRITES, /* waiting for next manual write */
238 AWS_H2_DATA_ENCODE_ONGOING_WINDOW_STALLED, /* stalled due to reduced window size */
239 };
240
241 /* When window size is too small to fit the possible padding into it, we stop sending data and wait for WINDOW_UPDATE */
242 #define AWS_H2_MIN_WINDOW_SIZE (256)
243
244 /* Private functions called from tests... */
245
246 AWS_EXTERN_C_BEGIN
247
248 AWS_HTTP_API
249 struct aws_http_connection *aws_http_connection_new_http2_server(
250 struct aws_allocator *allocator,
251 bool manual_window_management,
252 size_t initial_window_size,
253 const struct aws_http2_connection_options *http2_options);
254
255 AWS_HTTP_API
256 struct aws_http_connection *aws_http_connection_new_http2_client(
257 struct aws_allocator *allocator,
258 bool manual_window_management,
259 size_t initial_window_size,
260 const struct aws_http2_connection_options *http2_options);
261
262 AWS_EXTERN_C_END
263
264 /* Private functions called from multiple .c files... */
265
266 /**
267 * Enqueue outgoing frame.
268 * Connection takes ownership of frame.
269 * Frames are sent into FIFO order.
270 * Do not enqueue DATA frames, these are sent by other means when the frame queue is empty.
271 */
272 void aws_h2_connection_enqueue_outgoing_frame(struct aws_h2_connection *connection, struct aws_h2_frame *frame);
273
274 /**
275 * Invoked immediately after a stream enters the CLOSED state.
276 * The connection will remove the stream from its "active" datastructures,
277 * guaranteeing that no further decoder callbacks are invoked on the stream.
278 *
279 * This should NOT be invoked in the case of a "Connection Error",
280 * though a "Stream Error", in which a RST_STREAM is sent and the stream
281 * is closed early, would invoke this.
282 */
283 int aws_h2_connection_on_stream_closed(
284 struct aws_h2_connection *connection,
285 struct aws_h2_stream *stream,
286 enum aws_h2_stream_closed_when closed_when,
287 int aws_error_code);
288
289 /**
290 * Send RST_STREAM and close a stream reserved via PUSH_PROMISE.
291 */
292 int aws_h2_connection_send_rst_and_close_reserved_stream(
293 struct aws_h2_connection *connection,
294 uint32_t stream_id,
295 uint32_t h2_error_code);
296
297 /**
298 * Error happens while writing into channel, shutdown the connection. Only called within the eventloop thread
299 */
300 void aws_h2_connection_shutdown_due_to_write_err(struct aws_h2_connection *connection, int error_code);
301
302 /**
303 * Try to write outgoing frames, if the outgoing-frames-task isn't scheduled, run it immediately.
304 */
305 void aws_h2_try_write_outgoing_frames(struct aws_h2_connection *connection);
306
307 /* Helper to calculate the capped windows update delta */
308 uint32_t aws_h2_calculate_cap_window_update_delta(int64_t current_window, uint64_t pending_update_size);
309
310 #endif /* AWS_HTTP_H2_CONNECTION_H */