1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __mod_h2__h2_mplx__
18 #define __mod_h2__h2_mplx__
21 * The stream multiplexer. It pushes buckets from the connection
22 * thread to the stream threads and vice versa. It's thread-safe
25 * There is one h2_mplx instance for each h2_session, which sits on top
26 * of a particular httpd conn_rec. Input goes from the connection to
27 * the stream tasks. Output goes from the stream tasks to the connection,
30 * For each stream, there can be at most "H2StreamMaxMemSize" output bytes
31 * queued in the multiplexer. If a task thread tries to write more
32 * data, it is blocked until space becomes available.
34 * Writing input is never blocked. In order to use flow control on the input,
35 * the mplx can be polled for input data consumption.
39 struct apr_thread_mutex_t;
40 struct apr_thread_cond_t;
41 struct h2_bucket_beam;
47 struct apr_thread_cond_t;
51 #include <apr_queue.h>
53 typedef struct h2_mplx h2_mplx;
59 server_rec *s; /* server for master conn */
61 unsigned int event_pending;
63 unsigned int is_registered; /* is registered at h2_workers */
65 struct h2_ihash_t *streams; /* all streams currently processing */
66 struct h2_ihash_t *sredo; /* all streams that need to be re-started */
67 struct h2_ihash_t *shold; /* all streams done with task ongoing */
68 struct h2_ihash_t *spurge; /* all streams done, ready for destroy */
70 struct h2_iqueue *q; /* all stream ids that need to be started */
71 struct h2_ififo *readyq; /* all stream ids ready for output */
73 struct h2_ihash_t *redo_tasks; /* all tasks that need to be redone */
75 int max_streams; /* max # of concurrent streams */
76 int max_stream_started; /* highest stream id that started processing */
77 int tasks_active; /* # of tasks being processed from this mplx */
78 int limit_active; /* current limit on active tasks, dynamic */
79 int max_active; /* max, hard limit # of active tasks in a process */
80 apr_time_t last_idle_block; /* last time, this mplx entered IDLE while
81 * streams were ready */
82 apr_time_t last_limit_change; /* last time, worker limit changed */
83 apr_interval_time_t limit_change_interval;
85 apr_thread_mutex_t *lock;
86 struct apr_thread_cond_t *added_output;
87 struct apr_thread_cond_t *join_wait;
89 apr_size_t stream_max_mem;
91 apr_pool_t *spare_io_pool;
92 apr_array_header_t *spare_slaves; /* spare slave connections */
94 struct h2_workers *workers;
99 /*******************************************************************************
100 * Object lifecycle and information.
101 ******************************************************************************/
103 apr_status_t h2_mplx_child_init(apr_pool_t *pool, server_rec *s);
106 * Create the multiplexer for the given HTTP2 session.
107 * Implicitly has reference count 1.
109 h2_mplx *h2_mplx_create(conn_rec *c, server_rec *s, apr_pool_t *master,
110 struct h2_workers *workers);
113 * Decreases the reference counter of this mplx and waits for it
114 * to reached 0, destroy the mplx afterwards.
115 * This is to be called from the thread that created the mplx in
117 * @param m the mplx to be released and destroyed
118 * @param wait condition var to wait on for ref counter == 0
120 void h2_mplx_release_and_join(h2_mplx *m, struct apr_thread_cond_t *wait);
122 apr_status_t h2_mplx_pop_task(h2_mplx *m, struct h2_task **ptask);
124 void h2_mplx_task_done(h2_mplx *m, struct h2_task *task, struct h2_task **ptask);
127 * Shut down the multiplexer gracefully. Will no longer schedule new streams
128 * but let the ongoing ones finish normally.
129 * @return the highest stream id being/been processed
131 int h2_mplx_shutdown(h2_mplx *m);
133 int h2_mplx_is_busy(h2_mplx *m);
135 /*******************************************************************************
136 * IO lifetime of streams.
137 ******************************************************************************/
140 * Register a stream with the multiplexer. This transfers responisibility
141 * for lifetime and final destruction to mplx.
143 * @param mplx the multiplexer
144 * @param stream the h2 stream instance
146 void h2_mplx_stream_register(h2_mplx *mplx, struct h2_stream *stream);
149 * Lookup a stream by its id. Will only return active streams, not discarded ones.
150 * @param mplx the multiplexer
151 * @param id the stream identifier
152 * @return the stream or NULL
154 struct h2_stream *h2_mplx_stream_get(h2_mplx *mplx, int id);
157 * Notifies mplx that a stream has been completely handled on the main
158 * connection and is ready for cleanup.
160 * @param m the mplx itself
161 * @param stream the stream ready for cleanup
163 apr_status_t h2_mplx_stream_discard(h2_mplx *m, int stream_id);
166 * Waits on output data from any stream in this session to become available.
167 * Returns APR_TIMEUP if no data arrived in the given time.
169 apr_status_t h2_mplx_out_trywait(h2_mplx *m, apr_interval_time_t timeout,
170 struct apr_thread_cond_t *iowait);
172 apr_status_t h2_mplx_keep_active(h2_mplx *m, struct h2_stream *stream);
174 /*******************************************************************************
176 ******************************************************************************/
179 * Process a stream request.
181 * @param m the multiplexer
182 * @param stream_id the identifier of the stream
183 * @param r the request to be processed
184 * @param cmp the stream priority compare function
185 * @param ctx context data for the compare function
187 apr_status_t h2_mplx_process(h2_mplx *m, int stream_id, h2_stream_pri_cmp *cmp, void *ctx);
190 * Stream priorities have changed, reschedule pending requests.
192 * @param m the multiplexer
193 * @param cmp the stream priority compare function
194 * @param ctx context data for the compare function
196 apr_status_t h2_mplx_reprioritize(h2_mplx *m, h2_stream_pri_cmp *cmp, void *ctx);
198 typedef apr_status_t stream_ev_callback(void *ctx, struct h2_stream *stream);
201 * Check if the multiplexer has events for the master connection pending.
202 * @return != 0 iff there are events pending
204 int h2_mplx_has_master_events(h2_mplx *m);
207 * Dispatch events for the master connection, such as
208 ± @param m the multiplexer
209 * @param on_resume new output data has arrived for a suspended stream
210 * @param ctx user supplied argument to invocation.
212 apr_status_t h2_mplx_dispatch_master_events(h2_mplx *m,
213 stream_ev_callback *on_resume,
216 int h2_mplx_awaits_data(h2_mplx *m);
218 typedef int h2_mplx_stream_cb(struct h2_stream *s, void *ctx);
220 apr_status_t h2_mplx_stream_do(h2_mplx *m, h2_mplx_stream_cb *cb, void *ctx);
222 /*******************************************************************************
223 * Output handling of streams.
224 ******************************************************************************/
227 * Opens the output for the given stream with the specified response.
229 apr_status_t h2_mplx_out_open(h2_mplx *mplx, int stream_id,
230 struct h2_bucket_beam *beam);
232 /*******************************************************************************
233 * h2_mplx list Manipulation.
234 ******************************************************************************/
237 * The magic pointer value that indicates the head of a h2_mplx list
238 * @param b The mplx list
239 * @return The magic pointer value
241 #define H2_MPLX_LIST_SENTINEL(b) APR_RING_SENTINEL((b), h2_mplx, link)
244 * Determine if the mplx list is empty
245 * @param b The list to check
246 * @return true or false
248 #define H2_MPLX_LIST_EMPTY(b) APR_RING_EMPTY((b), h2_mplx, link)
251 * Return the first mplx in a list
252 * @param b The list to query
253 * @return The first mplx in the list
255 #define H2_MPLX_LIST_FIRST(b) APR_RING_FIRST(b)
258 * Return the last mplx in a list
259 * @param b The list to query
260 * @return The last mplx int he list
262 #define H2_MPLX_LIST_LAST(b) APR_RING_LAST(b)
265 * Insert a single mplx at the front of a list
266 * @param b The list to add to
267 * @param e The mplx to insert
269 #define H2_MPLX_LIST_INSERT_HEAD(b, e) do { \
270 h2_mplx *ap__b = (e); \
271 APR_RING_INSERT_HEAD((b), ap__b, h2_mplx, link); \
275 * Insert a single mplx at the end of a list
276 * @param b The list to add to
277 * @param e The mplx to insert
279 #define H2_MPLX_LIST_INSERT_TAIL(b, e) do { \
280 h2_mplx *ap__b = (e); \
281 APR_RING_INSERT_TAIL((b), ap__b, h2_mplx, link); \
285 * Get the next mplx in the list
286 * @param e The current mplx
287 * @return The next mplx
289 #define H2_MPLX_NEXT(e) APR_RING_NEXT((e), link)
291 * Get the previous mplx in the list
292 * @param e The current mplx
293 * @return The previous mplx
295 #define H2_MPLX_PREV(e) APR_RING_PREV((e), link)
298 * Remove a mplx from its list
299 * @param e The mplx to remove
301 #define H2_MPLX_REMOVE(e) APR_RING_REMOVE((e), link)
303 /*******************************************************************************
304 * h2_mplx DoS protection
305 ******************************************************************************/
308 * Master connection has entered idle mode.
309 * @param m the mplx instance of the master connection
310 * @return != SUCCESS iff connection should be terminated
312 apr_status_t h2_mplx_idle(h2_mplx *m);
314 #endif /* defined(__mod_h2__h2_mplx__) */