1 /* Copyright 2015 greenbytes GmbH (https://www.greenbytes.de)
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef __mod_h2__h2_mplx__
17 #define __mod_h2__h2_mplx__
20 * The stream multiplexer. It pushes buckets from the connection
21 * thread to the stream task threads and vice versa. It's thread-safe
24 * There is one h2_mplx instance for each h2_session, which sits on top
25 * of a particular httpd conn_rec. Input goes from the connection to
26 * the stream tasks. Output goes from the stream tasks to the connection,
29 * For each stream, there can be at most "H2StreamMaxMemSize" output bytes
30 * queued in the multiplexer. If a task thread tries to write more
31 * data, it is blocked until space becomes available.
33 * Writing input is never blocked. In order to use flow control on the input,
34 * the mplx can be polled for input data consumption.
38 struct apr_thread_mutex_t;
39 struct apr_thread_cond_t;
45 struct apr_thread_cond_t;
52 typedef struct h2_mplx h2_mplx;
56 APR_RING_ENTRY(h2_mplx) link;
57 volatile apr_uint32_t refs;
60 apr_bucket_alloc_t *bucket_alloc;
62 struct h2_task_queue *q;
63 struct h2_io_set *stream_ios;
64 struct h2_io_set *ready_ios;
66 apr_thread_mutex_t *lock;
67 struct apr_thread_cond_t *added_output;
68 struct apr_thread_cond_t *join_wait;
71 apr_size_t stream_max_mem;
73 apr_pool_t *spare_pool; /* spare pool, ready for next stream */
74 struct h2_stream_set *closed; /* streams closed, but task ongoing */
75 struct h2_workers *workers;
76 int file_handles_allowed;
79 /*******************************************************************************
80 * Object lifecycle and information.
81 ******************************************************************************/
84 * Create the multiplexer for the given HTTP2 session.
85 * Implicitly has reference count 1.
87 h2_mplx *h2_mplx_create(conn_rec *c, apr_pool_t *master,
88 struct h2_workers *workers);
91 * Increase the reference counter of this mplx.
93 void h2_mplx_reference(h2_mplx *m);
96 * Decreases the reference counter of this mplx.
98 void h2_mplx_release(h2_mplx *m);
100 * Decreases the reference counter of this mplx and waits for it
101 * to reached 0, destroy the mplx afterwards.
102 * This is to be called from the thread that created the mplx in
104 * @param m the mplx to be released and destroyed
105 * @param wait condition var to wait on for ref counter == 0
107 apr_status_t h2_mplx_release_and_join(h2_mplx *m, struct apr_thread_cond_t *wait);
110 * Aborts the multiplexer. It will answer all future invocation with
111 * APR_ECONNABORTED, leading to early termination of ongoing tasks.
113 void h2_mplx_abort(h2_mplx *mplx);
115 void h2_mplx_task_done(h2_mplx *m, int stream_id);
117 /*******************************************************************************
118 * IO lifetime of streams.
119 ******************************************************************************/
121 * Prepares the multiplexer to handle in-/output on the given stream id.
123 struct h2_stream *h2_mplx_open_io(h2_mplx *mplx, int stream_id);
126 * Ends cleanup of a stream in sync with execution thread.
128 apr_status_t h2_mplx_cleanup_stream(h2_mplx *m, struct h2_stream *stream);
130 /* Return != 0 iff the multiplexer has data for the given stream.
132 int h2_mplx_out_has_data_for(h2_mplx *m, int stream_id);
135 * Waits on output data from any stream in this session to become available.
136 * Returns APR_TIMEUP if no data arrived in the given time.
138 apr_status_t h2_mplx_out_trywait(h2_mplx *m, apr_interval_time_t timeout,
139 struct apr_thread_cond_t *iowait);
141 /*******************************************************************************
143 ******************************************************************************/
146 * Perform the task on the given stream.
148 apr_status_t h2_mplx_do_task(h2_mplx *mplx, struct h2_task *task);
150 struct h2_task *h2_mplx_pop_task(h2_mplx *mplx, int *has_more);
152 apr_status_t h2_mplx_create_task(h2_mplx *mplx, struct h2_stream *stream);
154 /*******************************************************************************
155 * Input handling of streams.
156 ******************************************************************************/
159 * Reads a buckets for the given stream_id. Will return ARP_EAGAIN when
160 * called with APR_NONBLOCK_READ and no data present. Will return APR_EOF
161 * when the end of the stream input has been reached.
162 * The condition passed in will be used for blocking/signalling and will
163 * be protected by the mplx's own mutex.
165 apr_status_t h2_mplx_in_read(h2_mplx *m, apr_read_type_e block,
166 int stream_id, apr_bucket_brigade *bb,
167 struct apr_thread_cond_t *iowait);
170 * Appends data to the input of the given stream. Storage of input data is
171 * not subject to flow control.
173 apr_status_t h2_mplx_in_write(h2_mplx *mplx, int stream_id,
174 apr_bucket_brigade *bb);
177 * Closes the input for the given stream_id.
179 apr_status_t h2_mplx_in_close(h2_mplx *m, int stream_id);
182 * Returns != 0 iff the input for the given stream has been closed. There
183 * could still be data queued, but it can be read without blocking.
185 int h2_mplx_in_has_eos_for(h2_mplx *m, int stream_id);
188 * Callback invoked for every stream that had input data read since
189 * the last invocation.
191 typedef void h2_mplx_consumed_cb(void *ctx, int stream_id, apr_size_t consumed);
194 * Invoke the callback for all streams that had bytes read since the last
195 * call to this function. If no stream had input data consumed, the callback
197 * Returns APR_SUCCESS when an update happened, APR_EAGAIN if no update
200 apr_status_t h2_mplx_in_update_windows(h2_mplx *m,
201 h2_mplx_consumed_cb *cb, void *ctx);
203 /*******************************************************************************
204 * Output handling of streams.
205 ******************************************************************************/
208 * Get a stream whose response is ready for submit. Will set response and
209 * any out data available in stream.
210 * @param m the mplxer to get a response from
211 * @param bb the brigade to place any existing repsonse body data into
213 struct h2_stream *h2_mplx_next_submit(h2_mplx *m,
214 struct h2_stream_set *streams);
217 * Reads output data from the given stream. Will never block, but
218 * return APR_EAGAIN until data arrives or the stream is closed.
220 apr_status_t h2_mplx_out_readx(h2_mplx *mplx, int stream_id,
221 h2_io_data_cb *cb, void *ctx,
222 apr_size_t *plen, int *peos);
225 * Opens the output for the given stream with the specified response.
227 apr_status_t h2_mplx_out_open(h2_mplx *mplx, int stream_id,
228 struct h2_response *response,
229 ap_filter_t* filter, apr_bucket_brigade *bb,
230 struct apr_thread_cond_t *iowait);
233 * Append the brigade to the stream output. Might block if amount
234 * of bytes buffered reaches configured max.
235 * @param stream_id the stream identifier
236 * @param filter the apache filter context of the data
237 * @param bb the bucket brigade to append
238 * @param iowait a conditional used for block/signalling in h2_mplx
240 apr_status_t h2_mplx_out_write(h2_mplx *mplx, int stream_id,
241 ap_filter_t* filter, apr_bucket_brigade *bb,
242 struct apr_thread_cond_t *iowait);
245 * Closes the output stream. Readers of this stream will get all pending
246 * data and then only APR_EOF as result.
248 apr_status_t h2_mplx_out_close(h2_mplx *m, int stream_id);
250 /*******************************************************************************
251 * h2_mplx list Manipulation.
252 ******************************************************************************/
255 * The magic pointer value that indicates the head of a h2_mplx list
256 * @param b The mplx list
257 * @return The magic pointer value
259 #define H2_MPLX_LIST_SENTINEL(b) APR_RING_SENTINEL((b), h2_mplx, link)
262 * Determine if the mplx list is empty
263 * @param b The list to check
264 * @return true or false
266 #define H2_MPLX_LIST_EMPTY(b) APR_RING_EMPTY((b), h2_mplx, link)
269 * Return the first mplx in a list
270 * @param b The list to query
271 * @return The first mplx in the list
273 #define H2_MPLX_LIST_FIRST(b) APR_RING_FIRST(b)
276 * Return the last mplx in a list
277 * @param b The list to query
278 * @return The last mplx int he list
280 #define H2_MPLX_LIST_LAST(b) APR_RING_LAST(b)
283 * Insert a single mplx at the front of a list
284 * @param b The list to add to
285 * @param e The mplx to insert
287 #define H2_MPLX_LIST_INSERT_HEAD(b, e) do { \
288 h2_mplx *ap__b = (e); \
289 APR_RING_INSERT_HEAD((b), ap__b, h2_mplx, link); \
293 * Insert a single mplx at the end of a list
294 * @param b The list to add to
295 * @param e The mplx to insert
297 #define H2_MPLX_LIST_INSERT_TAIL(b, e) do { \
298 h2_mplx *ap__b = (e); \
299 APR_RING_INSERT_TAIL((b), ap__b, h2_mplx, link); \
303 * Get the next mplx in the list
304 * @param e The current mplx
305 * @return The next mplx
307 #define H2_MPLX_NEXT(e) APR_RING_NEXT((e), link)
309 * Get the previous mplx in the list
310 * @param e The current mplx
311 * @return The previous mplx
313 #define H2_MPLX_PREV(e) APR_RING_PREV((e), link)
316 * Remove a mplx from its list
317 * @param e The mplx to remove
319 #define H2_MPLX_REMOVE(e) APR_RING_REMOVE((e), link)
322 #endif /* defined(__mod_h2__h2_mplx__) */